[FIX] base: prevent renaming the column for non stored field

Before this task, when creating the related field from the studio, it is by
default stored. When we change the field name in 'ir.model.fields' using ORM's
write method, it also renames the column in the table associated with the
field's model.

This is the expected behavior. But when someone tries to change the 'name' of a
non stored field, the column is not available in the associated model, and so
trying to rename the column results into traceback. This issue was probably
there for a while but came into light with the task 1985710. More info can
be found on the chatter of the task.

This commit fixes the issue by adding an extra check before renaming the table
column. The query will be executed only when the field is stored and thus
exists in the table.

closes odoo/odoo#33944

Signed-off-by: Raphael Collet (rco) <rco@openerp.com>
This commit is contained in:
Rishabh Jadia
2019-06-06 11:47:19 +00:00
parent f7b870ca1b
commit 7746621b92
+6 -5
View File
@@ -615,7 +615,7 @@ class IrModelFields(models.Model):
item._prepare_update()
if column_rename:
raise UserError(_('Can only rename one field at a time!'))
column_rename = (obj._table, item.name, vals['name'], item.index)
column_rename = (obj._table, item.name, vals['name'], item.index, item.store)
# We don't check the 'state', because it might come from the context
# (thus be set for multiple fields) and will be ignored anyway.
@@ -633,10 +633,11 @@ class IrModelFields(models.Model):
if column_rename:
# rename column in database, and its corresponding index if present
table, oldname, newname, index = column_rename
self._cr.execute('ALTER TABLE "%s" RENAME COLUMN "%s" TO "%s"' % (table, oldname, newname))
if index:
self._cr.execute('ALTER INDEX "%s_%s_index" RENAME TO "%s_%s_index"' % (table, oldname, table, newname))
table, oldname, newname, index, stored = column_rename
if stored:
self._cr.execute('ALTER TABLE "%s" RENAME COLUMN "%s" TO "%s"' % (table, oldname, newname))
if index:
self._cr.execute('ALTER INDEX "%s_%s_index" RENAME TO "%s_%s_index"' % (table, oldname, table, newname))
if column_rename or patched_models:
# setup models, this will reload all manual fields in registry