[FIX] models: check harder that default value is not NULL before setting it

When computing defaults we may end up with
a falsy value that is not None (e.g. '' or False)
That value will be cast to None when being
saved in the database, depending on the column type
(e.g. saving False on a many2one actually stores NULL).

Improve the test to consider the value being written
*after* that conversion, to *really* avoid nonsensical
and expensive queries such as:

    UPDATE table set col = NULL WHERE col IS NULL;
This commit is contained in:
Olivier Dony 2014-07-24 12:22:20 +02:00
parent eb08c12b09
commit 6d2fb3e3ae
1 changed files with 6 additions and 5 deletions

View File

@ -2415,13 +2415,14 @@ class BaseModel(object):
if column_name in defaults:
default = field.convert_to_write(defaults[column_name])
if default is not None:
_logger.debug("Table '%s': setting default value of new column %s",
self._table, column_name)
ss = self._columns[column_name]._symbol_set
ss = self._columns[column_name]._symbol_set
store_default = ss[1](default)
if store_default is not None:
_logger.debug("Table '%s': setting default value of new column %s to %r",
self._table, column_name, default)
query = 'UPDATE "%s" SET "%s"=%s WHERE "%s" is NULL' % (
self._table, column_name, ss[0], column_name)
cr.execute(query, (ss[1](default),))
cr.execute(query, (store_default,))
# this is a disgrace
cr.commit()