Hello Odooers, Odoogig and Odoo Lovers,
In this Odoo Tips Tutorial, I would like to explain how to use Unique _sql_constraints in Odoo
- SQL constraints will add to the table definition into database and implemented by PostgreSQL. it will be define using the class _sql_constraints attribute in odoo
- In general term Constrain means force to follow some particular action
- In odoo we use constrain to put some kind of restriction on record.
- For example, an employee’s identification number must be unique, means two employee can not have a same identification number
- So putting this type of unique restriction on records, we can use SQL Unique Constrain in odoo
- SQL is nothing but a database which is used by odoo
- SQL Unique Constrain will be written into python file as shown in below. it is a list of tuples with constraints name and error message.
#_sql_constraints #Modelconstraints #odooconstraints #odoo12.0
how to write _sql_constraints in odoo
_sql_constraints = [('name_of_field_unique', 'unique(name_of_field)',
_sql_constraints = [('identification_number_unique', 'unique(identification_number)',
'This Identification number is already assigned to other employee, please enter another number')]
- So now if you try to enter identification number which is already assigned to other employee then system will restrict you with error message. See below screen