changeset d03e797cd908 in trytond:default
details: https://hg.tryton.org/trytond?cmd=changeset;node=d03e797cd908
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
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 f9917483447a -r d03e797cd908 trytond/model/modelsql.py
--- a/trytond/model/modelsql.py Mon Jan 13 23:47:17 2020 +0100
+++ b/trytond/model/modelsql.py Mon Jan 13 23:58:14 2020 +0100
@@ -1588,6 +1588,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 f9917483447a -r d03e797cd908 trytond/tests/modelsql.py
--- a/trytond/tests/modelsql.py Mon Jan 13 23:47:17 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
@@ -120,13 +121,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 f9917483447a -r d03e797cd908 trytond/tests/test_modelsql.py
--- a/trytond/tests/test_modelsql.py Mon Jan 13 23:47:17 2020 +0100
+++ b/trytond/tests/test_modelsql.py Mon Jan 13 23:58:14 2020 +0100
@@ -478,7 +478,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)