[IMP] ir_sequence: (code, company_id) is unique, even for company_id IS NULL.

bzr revid: vmt@openerp.com-20110929134758-793vqith9pfcvs7w
This commit is contained in:
Vo Minh Thu 2011-09-29 15:47:58 +02:00
parent dc993b507e
commit 1381ac139e
1 changed files with 14 additions and 2 deletions

View File

@ -78,6 +78,20 @@ class ir_sequence(openerp.osv.osv.osv):
'padding' : 0,
}
def init(self, cr):
# CONSTRAINT/UNIQUE INDEX on (code, company_id)
# /!\ The unique constraint 'unique_name_company_id' is not sufficient, because SQL92
# only support field names in constraint definitions, and we need a function here:
# we need to special-case company_id to treat all NULL company_id as equal, otherwise
# we would allow duplicate (code, NULL) ir_sequences.
cr.execute("""
SELECT indexname FROM pg_indexes WHERE indexname =
'ir_sequence_unique_code_company_id_idx'""")
if not cr.fetchone():
cr.execute("""
CREATE UNIQUE INDEX ir_sequence_unique_code_company_id_idx
ON ir_sequence (code, (COALESCE(company_id,-1)))""")
def create(self, cr, uid, values, context=None):
values = self._add_missing_default_values(cr, uid, values, context)
go = super(ir_sequence, self).create \
@ -226,8 +240,6 @@ class ir_sequence(openerp.osv.osv.osv):
WHERE %s=%%s
AND active=true
AND (company_id in %%s or company_id is NULL)
ORDER BY company_id, id
LIMIT 1
%s
""" % (code_or_id, funw),
(sequence_code_or_id, tuple(company_ids)))