changeset c1c878ddacb2 in trytond:default
details: https://hg.tryton.org/trytond?cmd=changeset;node=c1c878ddacb2
description:
        Add check on ModelSQL for positive id

        issue7867
        review50631002
diffstat:

 CHANGELOG                 |   1 +
 trytond/model/modelsql.py |  20 +++++++++++++-------
 2 files changed, 14 insertions(+), 7 deletions(-)

diffs (47 lines):

diff -r 0a9cc207bd0f -r c1c878ddacb2 CHANGELOG
--- a/CHANGELOG Fri Dec 21 23:48:05 2018 +0100
+++ b/CHANGELOG Sat Dec 22 00:01:41 2018 +0100
@@ -1,3 +1,4 @@
+* Add check on ModelSQL for positive id
 * New API to read related fields
 * Remove implicit fields names in ModelStorage.read
 * Check read access on field in search domain (issue7766)
diff -r 0a9cc207bd0f -r c1c878ddacb2 trytond/model/modelsql.py
--- a/trytond/model/modelsql.py Fri Dec 21 23:48:05 2018 +0100
+++ b/trytond/model/modelsql.py Sat Dec 22 00:01:41 2018 +0100
@@ -160,8 +160,21 @@
 
     @classmethod
     def __setup__(cls):
+        cls._table = config.get('table', cls.__name__, default=cls._table)
+        if not cls._table:
+            cls._table = cls.__name__.replace('.', '_')
+
+        assert cls._table[-9:] != '__history', \
+            'Model _table %s cannot end with "__history"' % cls._table
+
         super(ModelSQL, cls).__setup__()
+
         cls._sql_constraints = []
+        if not callable(cls.table_query):
+            table = cls.__table__()
+            cls._sql_constraints.append(
+                ('id_positive', Check(table, table.id >= 0),
+                    "ID must be positive"))
         cls._order = [('id', 'ASC')]
         cls._sql_error_messages = {}
         if issubclass(cls, ModelView):
@@ -169,13 +182,6 @@
                     'history_revisions': RPC(),
                     })
 
-        cls._table = config.get('table', cls.__name__, default=cls._table)
-        if not cls._table:
-            cls._table = cls.__name__.replace('.', '_')
-
-        assert cls._table[-9:] != '__history', \
-            'Model _table %s cannot end with "__history"' % cls._table
-
     @classmethod
     def __table__(cls):
         if callable(cls.table_query):

Reply via email to