diff --git a/trytond/trytond/model/fields/many2one.py b/trytond/trytond/model/fields/many2one.py index c6406ff8d2..6394efe9e2 100644 --- a/trytond/trytond/model/fields/many2one.py +++ b/trytond/trytond/model/fields/many2one.py @@ -284,7 +284,10 @@ class Many2One(Field): if use_subquery: expression = column.in_(query) else: - query.where &= target_id == column + if query.where is None: + query.where = target_id == column + else: + query.where &= target_id == column expression = Exists(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 fb94fa4eaa..e85b8b528b 100644 --- a/trytond/trytond/tests/test_field_many2one.py +++ b/trytond/trytond/tests/test_field_many2one.py @@ -332,6 +332,27 @@ class SearchTestCaseMixin: ], query=True) self.assertIn(self._strategy, str(query)) + @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 FieldMany2OneTestCase( TestCase, CommonTestCaseMixin, SearchTestCaseMixin):