How to Use Constraint and SQL Constraint in Odoo 15
The document discusses setting constraints in Odoo 15 using both Python and SQL methods. Python constraints are defined with the @api.constrains decorator on specific fields, while SQL constraints are managed through the _sql_constraints attribute for ensuring data integrity. Key types of constraints include unique, not-null, and check constraints, each serving to validate data entry in the system.
How to Use Constraint and SQL Constraint in Odoo 15
1.
How to UseConstraints and
SQL Constraints in Odoo 15
www.cybrosys.com
2.
INTRODUCTION
Odoo helpsyou to set constraints to variants which we can perform using python and
model constraints. In odoo python constraints are specified along with methods.
This Slide will provide an insight on python and model constraints in Odoo 15.
In python, constraints are defined along with a method decorated with constraints().
3.
Odoo provides twoways to set up automatically verified invariants
Python constraints
SQL constraints
4.
A Pythonconstraint is defined as a method decorated with @api.constrains,
and invoked on a recordset.
The decorator specifies which fields are involved in the constraint, so that the
constraint is automatically evaluated when one of them is modified.
The method is expected to raise an exception if its invariant is not satisfied:
Python Constraints
5.
The field forwhich constraint is to be applied is specified along with the decorator constraints().
6.
Can provide multiplefield values as arguments in the function. Therefore, the function
gets invoked each time the value in the field gets modified/changed
7.
There are certainlimitations for using constraints()
● constrains() are not supported along with related fields. They can only be applied to simple
fields. For example related fields like partner_id.phone.
● constrains() can only be applied to fields that are included in the create and write call
because if the field is which is not contained in the view will not trigger a call to python
function.
8.
SQL constraintsare defined through the model using _sql_constraints.
The latter is assigned to a list of triples of strings (name, sql_definition, message),
where name is a valid SQL constraint name, sql_definition is a table_constraint_
expression, and message is the error message.
SQL Constraints
9.
The code isused to prevent the addition of records with the same email. Because it is
irrelevant to add the same email to blacklist twice. So for situations like this where you need
to apply constraints of this type the _sql_constraints comes into action.
10.
name -Name for the sql constraint(eg:'unique_email')
definition - Constraint to be applied on the table.(eg:unique (email))
message - Validation message to be displayed. (eg: Email address
already exists!)
11.
Check constraint:A check constraint is defined using the CHECK keyword. Furthermore, as
the name suggests it is used to check whether it satisfies a certain condition.
12.
Not-Null Constraints:NOT NULL keyword is used to specify not null constraints, which
means that certain values or fields cannot be NULL.
13.
Unique Constraint:Itis used along with a ‘unique’ keyword.The unique constraint helps
you to check whether a specified column in rows satisfies the unique constraint.
14.
For More
Details
Check ourcompany website for related blogs and Odoo
book.
Odoo Book V15
Check our YouTube channel for functional and technical
videos in Odoo.
How to Use Constraint and SQL Constraint? | Model
Constraints & SQL Constraints in Odoo 15