[IMP] model: when the orm create/update a table check if they are at least one row...
authorChristophe Matthieu <chm@odoo.com>
Tue, 15 Jul 2014 12:45:24 +0000 (14:45 +0200)
committerOlivier Dony <odo@openerp.com>
Thu, 17 Jul 2014 16:46:10 +0000 (18:46 +0200)
openerp/models.py

index 62e816a..ecc8c72 100644 (file)
@@ -2457,6 +2457,10 @@ class BaseModel(object):
 
             if create:
                 self._create_table(cr)
+                has_rows = False
+            else:
+                cr.execute('SELECT COUNT(1) FROM "%s"' % (self._table,))
+                has_rows = cr.fetchone()[0]
 
             cr.commit()
             if self._parent_store:
@@ -2575,7 +2579,8 @@ class BaseModel(object):
 
                             # if the field is required and hasn't got a NOT NULL constraint
                             if f.required and f_pg_notnull == 0:
-                                self._set_default_value_on_column(cr, k, context=context)
+                                if has_rows:
+                                    self._set_default_value_on_column(cr, k, context=context)
                                 # add the NOT NULL constraint
                                 try:
                                     cr.execute('ALTER TABLE "%s" ALTER COLUMN "%s" SET NOT NULL' % (self._table, k), log_exceptions=False)
@@ -2628,7 +2633,7 @@ class BaseModel(object):
                                 self._table, k, get_pg_type(f)[1])
 
                             # initialize it
-                            if not create:
+                            if has_rows:
                                 self._set_default_value_on_column(cr, k, context=context)
 
                             # remember the functions to call for the stored fields