From d32871570ac5c2c1f8cdcba734f6e68bbfe58be2 Mon Sep 17 00:00:00 2001 From: Raphael Collet Date: Wed, 17 Aug 2016 15:17:15 +0200 Subject: [PATCH] [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. --- openerp/addons/base/ir/ir_model.py | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/openerp/addons/base/ir/ir_model.py b/openerp/addons/base/ir/ir_model.py index 8962324f33e..e8b18c8019b 100644 --- a/openerp/addons/base/ir/ir_model.py +++ b/openerp/addons/base/ir/ir_model.py @@ -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)