Model constraints
Often, applications need to ensure data integrity and enforce validations to ensure that the data is complete and correct.
The PostgreSQL database manager supports many useful validations, such as avoiding duplicates or checking that values meet certain simple conditions. Odoo models can use the PostgreSQL constraints capabilities for this.
Some checks require more sophisticated logic and are better implemented as Python code. For these cases, we can use specific model methods that implement that Python constraint logic.
Let's learn more about these two possibilities.
SQL model constraints
SQL constraints are added to the database table definition and are enforced directly by PostgreSQL. They are declared using the _sql_constraints
class attribute.
It is a list of tuples, and each tuple has a format of (name, sql, message)
:
name
is the constraint identifier name.sql
is the PostgreSQL syntax for the constraint.message
is the...