From f8e37d6a808a302cda973bc0c6f2b275355ef74c Mon Sep 17 00:00:00 2001 From: Nicolas Seinlet Date: Fri, 18 Jan 2019 09:20:02 +0000 Subject: [PATCH] [IMP] improve overridden sql constraints support If module A define an sql constraints and module B, which depends on module A, override the sql constraint, we must ensure having only the last definition in constraints list. Having the two versions in the list makes the ORM continuously trying to drop the second version by the first one, then recreate the second version, when performing operations like an upgrade a module B. An illustration of this is the login_key on res.users, which is unique per login in base, but unique per (login, website) in website. closes odoo/odoo#30334 --- odoo/models.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/odoo/models.py b/odoo/models.py index 97787970d51..eca249f6200 100644 --- a/odoo/models.py +++ b/odoo/models.py @@ -519,7 +519,7 @@ class BaseModel(MetaModel('DummyModel', (object,), {'_register': False})): cls._inherits = {} cls._depends = {} cls._constraints = {} - cls._sql_constraints = [] + cls._sql_constraints = {} for base in reversed(cls.__bases__): if not getattr(base, 'pool', None): @@ -540,10 +540,12 @@ class BaseModel(MetaModel('DummyModel', (object,), {'_register': False})): # cons may override a constraint with the same function name cls._constraints[getattr(cons[0], '__name__', id(cons[0]))] = cons - cls._sql_constraints += base._sql_constraints + for cons in base._sql_constraints: + cls._sql_constraints[cons[0]] = cons cls._sequence = cls._sequence or (cls._table + '_id_seq') cls._constraints = list(cls._constraints.values()) + cls._sql_constraints = list(cls._sql_constraints.values()) # update _inherits_children of parent models for parent_name in cls._inherits: