changeset 57d3aec52a0e in trytond:5.6
details: https://hg.tryton.org/trytond?cmd=changeset&node=57d3aec52a0e
description:
        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
        (grafted from 661c377d1869f9d63e0f997d2a40b26f6fd0e738)
diffstat:

 trytond/model/fields/field.py |  34 ++++++++++++++++-----------
 trytond/tests/test_ir.py      |  52 +++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 72 insertions(+), 14 deletions(-)

diffs (124 lines):

diff -r 7fb2bfb52117 -r 57d3aec52a0e trytond/model/fields/field.py
--- a/trytond/model/fields/field.py     Wed Apr 21 23:47:30 2021 +0200
+++ b/trytond/model/fields/field.py     Sat Apr 24 19:27:07 2021 +0200
@@ -496,11 +496,30 @@
 
     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'
@@ -511,26 +530,13 @@
             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 convert_domain(self, domain, tables, Model):
         from trytond.ir.lang import get_parent_language
diff -r 7fb2bfb52117 -r 57d3aec52a0e trytond/tests/test_ir.py
--- a/trytond/tests/test_ir.py  Wed Apr 21 23:47:30 2021 +0200
+++ b/trytond/tests/test_ir.py  Sat Apr 24 19:27:07 2021 +0200
@@ -14,6 +14,58 @@
     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'
         pool = Pool()

Reply via email to