details: https://code.tryton.org/tryton/commit/ea30a0f2b4db
branch: 7.8
user: Cédric Krier <[email protected]>
date: Thu Jun 11 14:29:17 2026 +0200
description:
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)
diffstat:
trytond/trytond/model/fields/many2one.py | 5 ++++-
trytond/trytond/tests/test_field_many2one.py | 21 +++++++++++++++++++++
2 files changed, 25 insertions(+), 1 deletions(-)
diffs (46 lines):
diff -r 4d8ad0b886a6 -r ea30a0f2b4db trytond/trytond/model/fields/many2one.py
--- a/trytond/trytond/model/fields/many2one.py Sun May 10 11:44:04 2026 +0200
+++ b/trytond/trytond/model/fields/many2one.py Thu Jun 11 14:29:17 2026 +0200
@@ -281,7 +281,10 @@
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 -r 4d8ad0b886a6 -r ea30a0f2b4db
trytond/trytond/tests/test_field_many2one.py
--- a/trytond/trytond/tests/test_field_many2one.py Sun May 10 11:44:04
2026 +0200
+++ b/trytond/trytond/tests/test_field_many2one.py Thu Jun 11 14:29:17
2026 +0200
@@ -332,6 +332,27 @@
], 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):