From 42cb4a0f53fe53ead5870e082f43369a26a999ca Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?C=C3=A9dric=20Krier?= Date: Sat, 24 Apr 2021 19:27:07 +0200 Subject: [PATCH] Set name clause on join condition instead of subquery The name is a column from a table that is not available in the subquery. issue10257 review346001004 --- trytond/model/fields/field.py | 34 +++++++++++++---------- trytond/tests/test_ir.py | 52 +++++++++++++++++++++++++++++++++++ 2 files changed, 72 insertions(+), 14 deletions(-) diff --git a/trytond/model/fields/field.py b/trytond/model/fields/field.py index b414d8b777..5505398c60 100644 --- a/trytond/model/fields/field.py +++ b/trytond/model/fields/field.py @@ -508,11 +508,30 @@ class FieldTranslate(Field): def _get_translation_join(self, Model, name, translation, model, table, from_, language): + if backend.name == 'postgresql' and _sql_version >= (1, 1, 0): + query = translation.select( + translation.res_id.as_('res_id'), + translation.value.as_('value'), + translation.name.as_('name'), + distinct=True, + distinct_on=[translation.res_id, translation.name], + order_by=[ + translation.res_id, + translation.name, + translation.id.desc]) + else: + query = translation.select( + translation.res_id.as_('res_id'), + Min(translation.value).as_('value'), + translation.name.as_('name'), + group_by=[translation.res_id, translation.name]) if Model.__name__ == 'ir.model': name_ = Concat(Concat(table.model, ','), name) type_ = 'model' res_id = -1 elif Model.__name__ == 'ir.model.field': + from_ = from_.join(model, 'LEFT', + condition=model.id == table.model) name_ = Concat(Concat(model.model, ','), table.name) if name == 'field_description': type_ = 'field' @@ -523,26 +542,13 @@ class FieldTranslate(Field): name_ = '%s,%s' % (Model.__name__, name) type_ = 'model' res_id = table.id - if backend.name == 'postgresql' and _sql_version >= (1, 1, 0): - query = translation.select( - translation.res_id.as_('res_id'), - translation.value.as_('value'), - distinct=True, - distinct_on=[translation.res_id], - order_by=[translation.res_id, translation.id.desc]) - else: - query = translation.select( - translation.res_id.as_('res_id'), - Min(translation.value).as_('value'), - group_by=[translation.res_id]) query.where = ( (translation.lang == language) & (translation.type == type_) - & (translation.name == name_) & (translation.fuzzy == Literal(False)) ) return query, from_.join(query, 'LEFT', - condition=(query.res_id == res_id)) + condition=(query.res_id == res_id) & (query.name == name_)) def _get_translation_column(self, Model, name): from trytond.ir.lang import get_parent_language diff --git a/trytond/tests/test_ir.py b/trytond/tests/test_ir.py index a5add9d0e2..77c1a71aad 100644 --- a/trytond/tests/test_ir.py +++ b/trytond/tests/test_ir.py @@ -18,6 +18,58 @@ class IrTestCase(ModuleTestCase): 'Test ir module' module = 'ir' + @with_transaction() + def test_model_search_name(self): + "Test searching on name of model" + pool = Pool() + Model = pool.get('ir.model') + + record, = Model.search([ + ('name', '=', "Language"), + ('module', '=', 'ir'), + ]) + self.assertEqual(record.name, "Language") + + @with_transaction() + def test_model_search_order(self): + "Test searching and ordering on name of model" + pool = Pool() + Model = pool.get('ir.model') + + records = Model.search([ + ('name', 'in', ["Language", "Module"]), + ('module', '=', 'ir'), + ], + order=[('name', 'ASC')]) + self.assertEqual([r.name for r in records], ["Language", "Module"]) + + @with_transaction() + def test_model_field_search_description(self): + "Test searching on description of model field" + pool = Pool() + ModelField = pool.get('ir.model.field') + + field, = ModelField.search([ + ('field_description', '=', "Name"), + ('model.model', '=', 'ir.lang'), + ('module', '=', 'ir'), + ]) + self.assertEqual(field.field_description, "Name") + + @with_transaction() + def test_model_field_search_order_description(self): + "Test searching and ordering on description of model field" + pool = Pool() + ModelField = pool.get('ir.model.field') + + fields = ModelField.search([ + ('field_description', 'in', ["Name", "Code"]), + ('model.model', '=', 'ir.lang'), + ('module', '=', 'ir'), + ]) + self.assertEqual( + [f.field_description for f in fields], ["Code", "Name"]) + @with_transaction() def test_sequence_substitutions(self): 'Test Sequence Substitutions'