Sorting on lazy fields no longer possible in some cases
Follow-up of #10658 (closed)
Since this commit, searches with "OR'ed" clauses which trigger the usage of a Union query will crash if they are ordered on lazy fields.
Original message:
If we search on a domain such as the UNION is triggered, with a sort on a Reference field of the main table, the query fails. This is because
__searched_column
ignores Reference fields (because they are lazy-loaded by default).
This was not a problem before because even though the column was not in the searched columns, it always existed on the table, so a sort on the column did not cause any issue.
This is not specific to Reference fields, but may happen as soon as a search on a domain that triggers the UNION sorts on a lazy-loaded field.
The traceback in this case will look like (for instance):
File "/workspace/trytond/trytond/model/modelsql.py", line 1373, in search
cursor.execute(*select)
psycopg2.errors.UndefinedColumn: column a.for_object does not exist
LINE 1: ..." = 'model')) AND ("g"."fuzzy" = false)) ORDER BY "a"."for_o...
for a query with a domain as bellow, sorted on for_object
, which is a Reference field:
['OR',
('for_object', '=', str(self)),
[
('for_object.party', 'in',
(party, None), 'account.invoice'),
('for_object.contract', '=', self,
'contract.covered'),
]