[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:
parent
eb08c12b09
commit
6d2fb3e3ae
|
@ -2415,13 +2415,14 @@ class BaseModel(object):
|
||||||
if column_name in defaults:
|
if column_name in defaults:
|
||||||
default = field.convert_to_write(defaults[column_name])
|
default = field.convert_to_write(defaults[column_name])
|
||||||
|
|
||||||
if default is not None:
|
ss = self._columns[column_name]._symbol_set
|
||||||
_logger.debug("Table '%s': setting default value of new column %s",
|
store_default = ss[1](default)
|
||||||
self._table, column_name)
|
if store_default is not None:
|
||||||
ss = self._columns[column_name]._symbol_set
|
_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' % (
|
query = 'UPDATE "%s" SET "%s"=%s WHERE "%s" is NULL' % (
|
||||||
self._table, column_name, ss[0], column_name)
|
self._table, column_name, ss[0], column_name)
|
||||||
cr.execute(query, (ss[1](default),))
|
cr.execute(query, (store_default,))
|
||||||
# this is a disgrace
|
# this is a disgrace
|
||||||
cr.commit()
|
cr.commit()
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue