[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
This commit is contained in:
Nicolas Seinlet
2019-01-18 09:20:02 +00:00
parent 649ea4927e
commit f8e37d6a80
+4 -2
View File
@@ -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: