[FIX] base: extrapolate table name on uninstall for ir_model_constraints

When uninstalling a module, the model *may* be removed from the registry
before the removal of the model constraints; to prevent crashing in
those cases, a simple replace('.','_') on the model name should allow to
obtain the table name in these cases.
This commit is contained in:
Raphael Collet 2016-08-17 15:17:15 +02:00 committed by Damien Bouvy
parent 3846d35d02
commit d32871570a
No known key found for this signature in database
GPG Key ID: 1D0AB759B4B928E3
1 changed files with 8 additions and 5 deletions

View File

@ -541,7 +541,10 @@ class ir_model_constraint(Model):
ids.reverse()
for data in self.browse(cr, uid, ids, context):
model = data.model.model
model_obj = self.pool[model]
if model in self.pool:
table = self.pool[model]._table
else:
table = model.replace('.', '_')
name = openerp.tools.ustr(data.name)
typ = data.type
@ -555,17 +558,17 @@ class ir_model_constraint(Model):
if typ == 'f':
# test if FK exists on this table (it could be on a related m2m table, in which case we ignore it)
cr.execute("""SELECT 1 from pg_constraint cs JOIN pg_class cl ON (cs.conrelid = cl.oid)
WHERE cs.contype=%s and cs.conname=%s and cl.relname=%s""", ('f', name, model_obj._table))
WHERE cs.contype=%s and cs.conname=%s and cl.relname=%s""", ('f', name, table))
if cr.fetchone():
cr.execute('ALTER TABLE "%s" DROP CONSTRAINT "%s"' % (model_obj._table, name),)
cr.execute('ALTER TABLE "%s" DROP CONSTRAINT "%s"' % (table, name),)
_logger.info('Dropped FK CONSTRAINT %s@%s', name, model)
if typ == 'u':
# test if constraint exists
cr.execute("""SELECT 1 from pg_constraint cs JOIN pg_class cl ON (cs.conrelid = cl.oid)
WHERE cs.contype=%s and cs.conname=%s and cl.relname=%s""", ('u', name, model_obj._table))
WHERE cs.contype=%s and cs.conname=%s and cl.relname=%s""", ('u', name, table))
if cr.fetchone():
cr.execute('ALTER TABLE "%s" DROP CONSTRAINT "%s"' % (model_obj._table, name),)
cr.execute('ALTER TABLE "%s" DROP CONSTRAINT "%s"' % (table, name),)
_logger.info('Dropped CONSTRAINT %s@%s', name, model)
self.unlink(cr, uid, ids, context)