Skip to content

Commit 5376110

Browse files
committed
[FIX] util/orm: Fix non store field issue
Since 18.3, domain resolution goes strictly through ` _search` ``` domain.optimize_full(model) if not domain.is_true(): query.add_where(domain._to_sql(self, self._table, query)) ``` [domain]: https://github.com/odoo/odoo/blob/2f0f8e5e00685129b5bbe954117bc9f80a568e88/odoo/orm/models.py#L5365-L5371 optimize_full() leaves a leaf untouched when the field has no `search=` defined, so `_to_sql()` calls `field.to_sql()` directly. For non-stored fields this now raises: ``` File "/home/odoo/src/odoo/19.0/odoo/orm/models.py", line 5256, in _order_to_sql term = self._order_field_to_sql(alias, field_name, sql_direction, sql_nulls, query) File "/home/odoo/src/odoo/19.0/odoo/orm/models.py", line 5315, in _order_field_to_sql sql_field = self._field_to_sql(alias, field_name, query) File "/home/odoo/src/odoo/19.0/odoo/orm/models.py", line 2930, in _field_to_sql sql = field.to_sql(self, alias) File "/home/odoo/src/odoo/19.0/odoo/orm/fields_textual.py", line 395, in to_sql sql_field = super().to_sql(model, alias) File "/home/odoo/src/odoo/19.0/odoo/orm/fields.py", line 1216, in to_sql raise ValueError(f"Cannot convert {self} to SQL because it is not stored") ValueError: Cannot convert photovoltaics.installer.name to SQL because it is not stored ``` Previously (pre-18.3), such leaves were treated as always-true instead of raising: https://github.com/odoo/odoo/blob/d9c06a66356dd9d5a50821b8cde6194967353c18/odoo/osv/expression.py#L1180-L1187 Since we don't always know if a custom/third-party field is stored or searchable, assign a dummy `search=` returning an always-true leaf (e.g. [(1, '=', 1)]). This makes optimize_full() resolve the domain to is_true() before _to_sql() runs, restoring the old behavior and avoiding the ValueError. upg- 4578773
1 parent be32312 commit 5376110

1 file changed

Lines changed: 31 additions & 0 deletions

File tree

src/util/orm.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -556,6 +556,32 @@ def create(self, values=None, query=None, **kw):
556556
)
557557

558558

559+
def _patch_unsearchable_manual_fields(env, updated_field_ids):
560+
if not updated_field_ids:
561+
return
562+
563+
env.cr.execute(
564+
"""
565+
SELECT name, model
566+
FROM ir_model_fields
567+
WHERE store = false
568+
AND id IN %s
569+
""",
570+
[tuple(updated_field_ids)],
571+
)
572+
573+
def _search_field_dummy(self, operator, value):
574+
return [(1, "=", 1)]
575+
576+
for field_name, model_name in env.cr.fetchall():
577+
if model_name not in env:
578+
continue
579+
Model = env[model_name]
580+
field = Model._fields.get(field_name)
581+
if field is not None and not field.search:
582+
field.search = _search_field_dummy
583+
584+
559585
@contextmanager
560586
def custom_module_field_as_manual(env, rollback=True, do_flush=False):
561587
"""
@@ -812,6 +838,11 @@ def _add_magic_fields(self):
812838
)
813839
setup_models(env.cr)
814840

841+
# 4.1 Non-stored fields with no `search` method will otherwise raise an
842+
# ValueError (not stored, cannot convert to SQL).
843+
if version_gte("19.0"):
844+
_patch_unsearchable_manual_fields(env, updated_field_ids)
845+
815846
# 5. Do the operation.
816847
yield
817848

0 commit comments

Comments
 (0)