Odoo Help

1

CHECK sql_constraints

By
René Schuster
on 4/17/14, 9:34 AM 2,333 views

I'm trying to weaken a sql unique constraint.
Let's say I have a Human model with name and gender fields. And the name must be unique.

sql_constraints = [('name_unique', UNIQUE(name), 'Error: Name must be unique!')]

What I want is, that the name has to be unique if the gender is female.
How to achieve that?

Can I do something like:

sql_constraints = [('human_check', CHECK(gender='female' AND UNIQUE(name) OR (gender='male'), 'Error: Females must have unique names!')]

Or should I write a custom constraint?
Any other suggestions? (Like splitting the model into Males and Females)

Thanks.

1
Jagdish Panchal
On 4/17/14, 9:41 AM

Hi

Try this

_sql_constraints = [
    ('name_unique', 'CHECK (gender='female')',  'Error: Name must be unique!'),

]

Thx.

I have not tried that yet, but I'm quite sure this will only allow female humans.

René Schuster
on 4/17/14, 9:44 AM
0

Hi, Try this.

_sql_constraints = [
    ('name_unique', 'CHECK (gender='female' AND name IS UNIQUE)',  'Error: Name must be unique!'),
]

Just a guess. Better go for custom _constraint.

I like both your suggestions.

But it seems that your sql constraint is not working (yet, no error).

René Schuster
on 4/23/14, 7:24 AM

About This Community

This platform is for beginners and experts willing to share their Odoo knowledge. It's not a forum to discuss ideas, but a knowledge base of questions and their answers.

Register

Odoo Training Center

Access to our E-learning platform and experience all Odoo Apps through learning videos, exercises and Quizz.

Test it now

Question tools

0 follower(s)

Stats

Asked: 4/17/14, 9:34 AM
Seen: 2333 times
Last updated: 3/16/15, 8:10 AM