changeset c4661e57a7c4 in trytond:5.2
details: https://hg.tryton.org/trytond?cmd=changeset;node=c4661e57a7c4
description:
        Apply Exclude where clause when searching other records

        When we simulate the Exclude constraints, we must apply the where 
clause also
        to existing records.

        issue8954
        review284651002
        (grafted from d03e797cd90862ab3be45aebd0175654aeecc7de)
diffstat:

 trytond/model/modelsql.py      |   2 ++
 trytond/tests/modelsql.py      |   9 ++++++++-
 trytond/tests/test_modelsql.py |  15 ++++++++++++++-
 3 files changed, 24 insertions(+), 2 deletions(-)

diffs (68 lines):

diff -r dc82fd52dbbe -r c4661e57a7c4 trytond/model/modelsql.py
--- a/trytond/model/modelsql.py Thu Jan 09 22:08:02 2020 +0100
+++ b/trytond/model/modelsql.py Mon Jan 13 23:58:14 2020 +0100
@@ -1590,6 +1590,8 @@
                                 clause &= Literal(False)
                             clause &= operator(column, value)
                         where |= clause
+                    if isinstance(sql, Exclude) and sql.where:
+                        where &= sql.where
                     cursor.execute(
                         *table.select(table.id, where=where, limit=1))
                     if cursor.fetchone():
diff -r dc82fd52dbbe -r c4661e57a7c4 trytond/tests/modelsql.py
--- a/trytond/tests/modelsql.py Thu Jan 09 22:08:02 2020 +0100
+++ b/trytond/tests/modelsql.py Mon Jan 13 23:58:14 2020 +0100
@@ -1,5 +1,6 @@
 # This file is part of Tryton.  The COPYRIGHT file at the top level of
 # this repository contains the full copyright notices and license terms.
+from sql import Literal
 from sql.operators import Equal
 
 from trytond.model import ModelSQL, fields, Check, Unique, Exclude
@@ -111,13 +112,19 @@
     "ModelSQL with exclude constraint"
     __name__ = 'test.modelsql.exclude'
     value = fields.Integer("Value")
+    condition = fields.Boolean("Condition")
+
+    @classmethod
+    def default_condition(cls):
+        return True
 
     @classmethod
     def __setup__(cls):
         super(ModelExclude, cls).__setup__()
         t = cls.__table__()
         cls._sql_constraints = [
-            ('exclude', Exclude(t, (t.value, Equal), where=t.value > 0),
+            ('exclude', Exclude(t, (t.value, Equal),
+                    where=t.condition == Literal(True)),
                 "Value must be unique."),
             ]
 
diff -r dc82fd52dbbe -r c4661e57a7c4 trytond/tests/test_modelsql.py
--- a/trytond/tests/test_modelsql.py    Thu Jan 09 22:08:02 2020 +0100
+++ b/trytond/tests/test_modelsql.py    Mon Jan 13 23:58:14 2020 +0100
@@ -467,7 +467,20 @@
         pool = Pool()
         Model = pool.get('test.modelsql.exclude')
 
-        records = Model.create([{'value': -1}, {'value': -1}])
+        records = Model.create([{'value': 1, 'condition': False}] * 2)
+
+        self.assertEqual(len(records), 2)
+
+    @with_transaction()
+    def test_constraint_exclude_exclusion_mixed(self):
+        "Test exclude constraint exclusion mixed"
+        pool = Pool()
+        Model = pool.get('test.modelsql.exclude')
+
+        records = Model.create([
+                {'value': 1, 'condition': False},
+                {'value': 1, 'condition': True},
+                ])
 
         self.assertEqual(len(records), 2)
 

Reply via email to