From dc482bf2a331e5b73fecd27acc07397337bef24f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?C=C3=A9dric=20Krier?= Date: Thu, 11 Jun 2026 14:29:17 +0200 Subject: [PATCH] Support query without where clause when searching on Many2One with where operator When not using subquery but EXISTS and having a target domain converted into UNION, the query result does not have a 'where' clause. Closes #14889 (grafted from 96982406e129d1e063c1a332289376f45e586ecc) --HG-- branch : 7.0 --- trytond/trytond/model/fields/many2one.py | 5 ++++- trytond/trytond/tests/test_field_many2one.py | 21 ++++++++++++++++++++ 2 files changed, 25 insertions(+), 1 deletion(-) diff --git a/trytond/trytond/model/fields/many2one.py b/trytond/trytond/model/fields/many2one.py index f33edcca9e..e20595fe7b 100644 --- a/trytond/trytond/model/fields/many2one.py +++ b/trytond/trytond/model/fields/many2one.py @@ -250,7 +250,10 @@ class Many2One(Field): target_id, = query.columns if isinstance(target_id, As): target_id = target_id.expression - query.where &= target_id == column + if query.where is None: + query.where = target_id == column + else: + query.where &= target_id == column expression = column.in_(query) if operator.startswith('not'): return ~expression diff --git a/trytond/trytond/tests/test_field_many2one.py b/trytond/trytond/tests/test_field_many2one.py index 88fdc59913..d46fcb0dd1 100644 --- a/trytond/trytond/tests/test_field_many2one.py +++ b/trytond/trytond/tests/test_field_many2one.py @@ -274,6 +274,27 @@ class FieldMany2OneTestCase(unittest.TestCase): self.assertEqual(record.target.context, 'foo') + @with_transaction() + def test_search_where_target_union(self): + "Test search on Many2One using where and UNION-ed query on target" + pool = Pool() + Target = pool.get('test.many2one_target') + Many2One = pool.get('test.many2one') + target1, target2 = Target.create([ + {'value': 1}, + {'value': 2}, + ]) + many2one1, many2one2 = Many2One.create([ + {'many2one': target1}, + {'many2one': target2}, + ]) + + many2ones = Many2One.search([ + ('many2one', 'where', + ['OR', ('value', '=', 1), ('write_uid.id', '=', -1)]), + ]) + self.assertListEqual(many2ones, [many2one1]) + class FieldMany2OneTreeTestCase(unittest.TestCase): "Test Field Many2One Tree"