[FIX] orm: do not drop foreign keys of transient models
authorMartin Trigaux <mat@openerp.com>
Mon, 15 Sep 2014 13:24:43 +0000 (15:24 +0200)
committerMartin Trigaux <mat@openerp.com>
Mon, 15 Sep 2014 13:24:53 +0000 (15:24 +0200)
During the update of a module, the existing foreign keys are dropped if they have a different ondelete_rule than the one specified on the field.
The foreign keys for many2one transiant -> non-transiant are created with cascade rule by default (see `m2o_add_foreign_key_checked` method) so the check needs to be realised in the same conditions.

openerp/osv/orm.py

index 9bf10df..07ed496 100644 (file)
@@ -3016,6 +3016,9 @@ class BaseModel(object):
             if len(constraints) == 1:
                 # Is it the right constraint?
                 cons, = constraints
+                if self.is_transient() and not dest_model.is_transient():
+                    # transient foreign keys are added as cascade by default
+                    ondelete = ondelete or 'cascade'
                 if cons['ondelete_rule'] != POSTGRES_CONFDELTYPES.get((ondelete or 'set null').upper(), 'a')\
                     or cons['foreign_table'] != dest_model._table:
                     # Wrong FK: drop it and recreate