ir.translation: select restriction must be an SQL constraint

select fields validate themselves in ORM, through _check_selection_field_value(),
which /retrieves/ the .selection list and checks if value to be written
is among possible ones. This, however, is very expensive for models that
have variable selection lists (eg. ones that require an SQL query).

In fact, for ir.translation, the query would be repeat for every single
write() to ir.translation. The 'lang' field is actually a reference for
res.lang.code (not the .id, sadly), so let SQL implement the constraint
for us.

bzr revid: xrg@linux.gr-20110608093254-ua66p5co6dc203zs
This commit is contained in:
P. Christeas 2011-06-08 12:32:54 +03:00 committed by P. Christeas
parent 28e3960004
commit 49fbcdb70a
2 changed files with 16 additions and 2 deletions

View File

@ -314,6 +314,12 @@ CREATE TABLE res_company (
primary key(id)
);
CREATE TABLE res_lang (
id serial PRIMARY KEY,
name VARCHAR(64) NOT NULL UNIQUE,
code VARCHAR(16) NOT NULL UNIQUE
);
CREATE TABLE ir_model_data (
id serial NOT NULL,
create_uid integer,

View File

@ -62,8 +62,12 @@ class ir_translation(osv.osv):
'src': fields.text('Source'),
'value': fields.text('Translation Value'),
}
_sql_constraints = [ ('lang_fkey_res_lang', 'FOREIGN KEY(lang) REFERENCES res_lang(code)',
'Language code of translation item must be among known languages' ), ]
def _auto_init(self, cr, context={}):
def _auto_init(self, cr, context=None):
super(ir_translation, self)._auto_init(cr, context)
# FIXME: there is a size limit on btree indexed values so we can't index src column with normal btree.
@ -88,7 +92,11 @@ class ir_translation(osv.osv):
cr.execute('CREATE INDEX ir_translation_ltn ON ir_translation (name, lang, type)')
cr.commit()
def _check_selection_field_value(self, cr, uid, field, value, context=None):
if field == 'lang':
return
return super(ir_translation, self)._check_selection_field_value(cr, uid, field, value, context=context)
@tools.cache(skiparg=3, multi='ids')
def _get_ids(self, cr, uid, name, tt, lang, ids):
translations = dict.fromkeys(ids, False)