0

I create a field with a type when I create a model:

wrong_field = fields.Char('Wrong')

now, I want to correct this field and migrate date to the new field. I found keywords "oldname" in odoo docs. so I change my field like this:

right_field = fields.Char('Right', oldname='wrong_field')

but it didn't work, the new field didn't have data.
why doesn't this work? How can I resolve it?
Thank you!

noble_man
  • 352
  • 3
  • 20
Yuhui Wang
  • 49
  • 2
  • 8

1 Answers1

0

I've resolved it. According to the following coding:

def update_db(self, model, columns):
    """ Update the database schema to implement this field.

        :param model: an instance of the field's model
        :param columns: a dict mapping column names to their configuration in database
        :return: ``True`` if the field must be recomputed on existing rows
    """
    if not self.column_type:
        return

    column = columns.get(self.name)
    if not column and hasattr(self, 'oldname'):
        # column not found; check whether it exists under its old name
        column = columns.get(self.oldname)
        if column:
            sql.rename_column(model._cr, model._table, self.oldname, self.name)

    # create/update the column, not null constraint, indexes
    self.update_db_column(model, column)
    self.update_db_notnull(model, column)
    self.update_db_index(model, column)

    return not column

The rigth_field must not on the table. Delete the right_field in the table from the database first, update again.

KbiR
  • 4,047
  • 6
  • 37
  • 103
Yuhui Wang
  • 49
  • 2
  • 8