Enable Dark Mode!
By: Archana V

Constraints In Odoo 16 ERP


In this blog, we are going to discuss the types of constraints in Odoo 16 .

Two Types of constraints are- 

1 . Python Constraints

2 . SQL Constraints

Python Constraints: 

Odoo helps you set constraints on the variants you can do with Python and model constraints. In Odoo, Python constraints are specified with methods.

This blog provides insight into Python and model constraints in Odoo 16.

In Python, constraints are defined with methods decorated with Constraints().

Assuming you need to add a constraint while saving the

record, you can use code to prevent the user from entering a negative number for the number.

from odoo.exceptions import ValidationError
def check_negative_number(self):
   for rec in self:
       if rec.number < 0:
           raise ValidationError("Number cannot be negative")

The field to apply the constraint to is specified with the decorator constraint ().

Multiple field values ??can be specified as function arguments. So the function is called whenever the value of the field changes or is modified. This is shown in the following command.

Here we are checking two fields, field1 and field 2.

@api.constrains(field1, field2)
def _check_values(self):
   for rec in self:
       if rec.field1 == rec.field2:
           raise ValidationError("field1 and field2 must be not be same")

However, using Constraints() has certain limitations:

1. Constraints() is not supported on related fields. Applicable only to simple fields. 

For example, related fields such as partner_id.phone.

2.Constraints() can only be applied to fields involved in create and write calls. This is because the Python function call is not triggered if the field is not included in the view.

This is python constraints, and now we can see the purpose of SQL Constraints in Odoo 16.

SQL Constraints:

The SQL Constraints, which are defined through the model attribute using the _sql_constraints, which also give the same purpose as python constraints that helps to set validations for the records and act as a reminder for the user for the workflow.
In Odoo,  _sql_constraints consist of three parts:-
* Name
* Definition
* Message
The code below is used to avoid adding records with the same email address. There is no point in adding the same email to the blacklist twice. _sql_constraints is useful in situations like this where you need to enforce this type of constraint.

_sql_constraints = [
('unique_email', 'unique (email)', 'Email address already exists!')

Name - the name of the SQL constraint (eg "unique_email")
definition - the constraint to apply to the table (eg unique (email))
message - the validation message to display. (e.g. email address already     exists!)
This is all about the two types of constraints in odoo 16.

Here we have to discuss Unique Constraints, Check Constraints & Not-Null Constraints.

Unique Constraint.

Used in conjunction with the "unique" keyword.

A unique constraint helps to check whether a particular column in a row satisfies a unique constraint.

Here is an example of a unique constraint:

_sql_constraints = [
   ('key_uniq', 'unique (key)', 'Key must be unique.')

Check Constraints.

A check constraint is defined using the CHECK keyword. As the name suggests, it also plays the role of checking whether certain conditions are met.

eg : 

_sql_constraints = [
        ('bom_quantity_zero', 'CHECK (product_quantity>=0)', 'All product quantities must be greater or equal to 0.')]

Not-Null Constraints.

The NOT NULL keyword is used to specify a non-null constraint. That is, certain values ??or fields cannot be NULL.


_sql_constraints = [
        ('sale_order_required_if_sale_line', "CHECK((sale_line_id IS NOT NULL AND sale_order_id IS NOT NULL) OR (sale_line_id IS NULL))", 'The Project should be linked to a Sale Order to select an Sale Order Items.'),

Now in this blog we are discussed all useful constraints available in Odoo.

If you need any assistance in odoo, we are online, please chat with us.


Leave a comment




Cybrosys Technologies Pvt. Ltd.
Neospace, Kinfra Techno Park
Kakkancherry, Calicut
Kerala, India - 673635



Cybrosys Technologies Pvt. Ltd.
1st Floor, Thapasya Building,
Infopark, Kakkanad,
Kochi, India - 682030.



Cybrosys Techno Solutions
The Estate, 8th Floor,
Dickenson Road,
Bangalore, India - 560042

Send Us A Message