How to Use Constraints and
SQL Constraints in Odoo 15
www.cybrosys.com
INTRODUCTION
 Odoo helps you 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().
Odoo provides two ways to set up automatically verified invariants
 Python constraints
 SQL constraints
 A Python constraint 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
The field for which constraint is to be applied is specified along with the decorator constraints().
Can provide multiple field values as arguments in the function. Therefore, the function
gets invoked each time the value in the field gets modified/changed
There are certain limitations 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.
 SQL constraints are 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
The code is used 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.
 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!)
 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.
 Not-Null Constraints: NOT NULL keyword is used to specify not null constraints, which
means that certain values or fields cannot be NULL.
 Unique Constraint:It is used along with a ‘unique’ keyword.The unique constraint helps
you to check whether a specified column in rows satisfies the unique constraint.
For More
Details
Check our company 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
Thank You

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
  • 16.