[FIX] ir.model.fields: better unlink
authorMartin Trigaux <mat@openerp.com>
Tue, 8 Jul 2014 11:55:48 +0000 (13:55 +0200)
committerMartin Trigaux <mat@openerp.com>
Tue, 8 Jul 2014 11:55:48 +0000 (13:55 +0200)
When droping a column, remove also the relation table in case of custom m2m field.
The relation table needs to be dropped otherwise an unremovable constraint to the targetted table is kept (and anyway is not needed anymore).

openerp/addons/base/ir/ir_model.py

index e7775b8..966f5e2 100644 (file)
@@ -312,6 +312,14 @@ class ir_model_fields(osv.osv):
             if column_name and (result and result[0] == 'r'):
                 cr.execute('ALTER table "%s" DROP column "%s" cascade' % (model._table, field.name))
             model._columns.pop(field.name, None)
+
+            # remove m2m relation table for custom fields
+            # we consider the m2m relation is only one way as it's not possible
+            # to specify the relation table in the interface for custom fields
+            # TODO master: maybe use ir.model.relations for custom fields
+            if field.state == 'manual' and field.ttype == 'many2many':
+                rel_name = self.pool[field.model]._all_columns[field.name].column._rel
+                cr.execute('DROP table "%s"' % (rel_name))
         return True
 
     def unlink(self, cr, user, ids, context=None):