From 6e6f12144de6147b5c2eccea2bc3d663775ef702 Mon Sep 17 00:00:00 2001 From: Christophe Matthieu Date: Tue, 15 Jul 2014 14:45:24 +0200 Subject: [PATCH] [IMP] model: when the orm create/update a table check if they are at least one row of the table to load column default values --- openerp/models.py | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/openerp/models.py b/openerp/models.py index 62e816aa6a4..ecc8c7263fa 100644 --- a/openerp/models.py +++ b/openerp/models.py @@ -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