Required field doesn't get unique default values when setting it to required later on
Affects | Status | Importance | Assigned to | Milestone | |
---|---|---|---|---|---|
Odoo Server (MOVED TO GITHUB) |
New
|
Undecided
|
Unassigned |
Bug Description
I created a field with a _defaults method. (see code below)
In this method was a uuid4() value.
After a while I decided that this field should become required. So, i added required=1 on the field definition and updated the database.
I expected that every existing record would get a unique UUID.
What I got was the same unique UUID for every record.
After a bit of investigation I found out where this happens:
http://
And indeed, the UPDATE SQL just writes 1 default value to all records.
In my case (UUID) this was a problem and something I didn't expect.
In other cases (like datetime.now()) this can be a less important problem.
Just a simple code example below:
# -*- coding: utf-8 -*-
from openerp.osv import fields, orm
from uuid import uuid4
class MyModel(orm.Model):
_name = 'my.model'
_columns = {
'state': fields.
}
def _get_default_
#TODO: use openerp sequences
return '%s_%s' % (cr.dbname, str(uuid4()))
_defaults = {
'state': 'draft',
}
Later I added:
_sql_
]
Then i got:
2014-04-23 10:05:38,042 5014 ERROR db openerp.sql_db: bad query: ALTER TABLE "my_model" ADD CONSTRAINT "my_model_
Traceback (most recent call last):
File "/home/
res = self._obj.
IntegrityError: could not create unique index "my_model_
DETAIL: Key (reference)