Odoo Help


This community 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.


How to use sql constraints in odoo

on 5/12/16, 1:11 AM 463 views

Hi im kiruba. Im new in odoo 9. I need how to ssql constraints are using in odoo. Let know any solution ur side

Kalpana Hemnani
On 5/12/16, 3:38 AM
Hi Kirubanidhi,
SQL constraints are used to specify rules for the data in a table.
If there is any violation between the constraint and the data action, the action is aborted by the constraint.
You can create constrain in your defined object as below:
list of (name, sql_definition, message) triples defining SQL constraints to execute when generating the backing table
For more reference check:https://www.odoo.com/documentation/8.0/reference/orm.html#module-openerp.api
On 5/13/16, 1:48 AM

_sql_constraints = [

('email_uniq', 'unique(email)', 'Email id is unique change your custom email id'),



def validate_email(self):

for obj in self:

if re.match("^.+\\@(\\[?)[a-zA-Z0-9\\-\\.]+\\.([a-zA-Z]{2,3}|[0-9]{1,3})(\\]?)$", obj.email) == None:

raise ValidationError("Please Provide valid Email Address: %s" % obj.email)

return True

Both constraints are working fine in my module.

On 5/12/16, 6:56 AM

you can use @api.constrains too


Akhil P Sivan

--Akhil P Sivan--
| 5 3 6
Kochi, India
--Akhil P Sivan--

Interested in exploring Odoo both technically & functionality wise.

email-id : akhilpsivan01@gmail.com

Akhil P Sivan
On 5/12/16, 5:48 AM


You may refer any addon in Odoo 9.0. 

For eg, in account module, goto account >> models >> account.py

_sql_constraints = [

('code_company_uniq', 'unique (code,company_id)', 'The code of the account must be unique per company !')


You can refer the usage.

Your Answer

Please try to give a substantial answer. If you wanted to comment on the question or answer, just use the commenting tool. Please remember that you can always revise your answers - no need to answer the same question twice. Also, please don't forget to vote - it really helps to select the best questions and answers!

About This Community

This community is for professionals and enthusiasts of our products and services. Read Guidelines

Question tools

1 follower(s)


Asked: 5/12/16, 1:11 AM
Seen: 463 times
Last updated: 5/13/16, 1:48 AM