From 84e9a67cdf78db94cb7a09543c1b7ac4ad19d8b4 Mon Sep 17 00:00:00 2001 From: Martin Trigaux Date: Tue, 8 Jul 2014 15:56:24 +0200 Subject: [PATCH] [FIX] orm: better removal of custom m2m fields orm: do not try to create ir.model.relation for custom m2m as self._module is either empty (for custom models), either the one of the last inheriting module (which is wrong). The field should be removed manually and should not be impacted by the uninstallation of modules. The removal of the relation table can be done when removing manually the custom field (see rev 6af3193). ir.model: when removing a model, drop the table with the CASCADE instruction. This will remove left constraints from remaining m2m tables. This means that dropping a table (either manually removing a custom model or uninstalling a module) will not drop the relation table for a custom m2m field. This is not ideal but better than the previous behaviour (which was to fail the DROP TABLE instruction and keep the table with a few columns and unconsistent data). --- openerp/addons/base/ir/ir_model.py | 2 +- openerp/osv/orm.py | 5 ++++- 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/openerp/addons/base/ir/ir_model.py b/openerp/addons/base/ir/ir_model.py index 966f5e2..8796f89 100644 --- a/openerp/addons/base/ir/ir_model.py +++ b/openerp/addons/base/ir/ir_model.py @@ -151,7 +151,7 @@ class ir_model(osv.osv): if result and result[0] == 'v': cr.execute('DROP view %s' % (model_pool._table,)) elif result and result[0] == 'r': - cr.execute('DROP TABLE %s' % (model_pool._table,)) + cr.execute('DROP TABLE %s CASCADE' % (model_pool._table,)) return True def unlink(self, cr, user, ids, context=None): diff --git a/openerp/osv/orm.py b/openerp/osv/orm.py index fc38169..1f2823b 100644 --- a/openerp/osv/orm.py +++ b/openerp/osv/orm.py @@ -3385,7 +3385,10 @@ class BaseModel(object): def _m2m_raise_or_create_relation(self, cr, f): m2m_tbl, col1, col2 = f._sql_names(self) - self._save_relation_table(cr, m2m_tbl) + # do not create relations for custom fields as they do not belong to a module + # they will be automatically removed when dropping the corresponding ir.model.field + if not f.string.startswith('x_'): + self._save_relation_table(cr, m2m_tbl) cr.execute("SELECT relname FROM pg_class WHERE relkind IN ('r','v') AND relname=%s", (m2m_tbl,)) if not cr.dictfetchall(): if not self.pool.get(f._obj): -- 1.7.10.4