What is the correct format for using Unique in _sql_constraints in OpenERP?

I tried unique in sql_constaints in OpenERP (Odoo) using two different methods using flower brackets {} or square brackets []. Both work great. Which one is correct?

_sql_constraints = { ('email_uniq', 'unique(email)', ' Please enter Unique Email id.') } 

(or)

 _sql_constraints = [ ('email_uniq', 'unique(email)', ' Please enter Unique Email id.') ] 

PS: But if I want to use more than the restriction, it only accepts square brackets [] like this example.

 _sql_constraints = [ ('email_uniq', 'unique(email)', ' Please enter Unique Email id.'), ('contact_uniq', 'unique(contact)', ' Please enter Unique Mobile no.') ] 

What is the reason for this?

+5
source share
2 answers

Correct is syntax with square brackets.

  • you can grep on _sql_constraints and see that it is always used

  • in the ORM code in openerp/models.py we see that the default value is an empty list:

  _sql_constraints = [] #... cls._local_sql_constraints = cls.__dict__.get('_sql_constraints', []) 
  1. Odoo 8.0 Documentation says:

a list (name, sql_definition, message) triples that define the SQL restrictions to execute when creating the support table.

In python2, you can get a list with the syntax [] .

The syntax {} either creates:

  • a dictionary , if it is empty {} , or there are key values ​​such as this: {'keyA': 'valueA', 'keyB': 'valueB'} ,
  • as from python 2.7 a set , if created in this way: {'value1', 'valueB', 42}
+7
source

In odoo, the syntax for sql constraint is a list (name, sql_definition, message) . eg

 _sql_constraints = [ ('name_uniq', 'unique(name)', 'Custom Warning Message'), ('contact_uniq', 'unique(contact)', 'Custom Warning Message') ] 

you can specify more than one sql constraint in the list of tuples.

when you define a public key dictionary and the python value treats it as given. and the set may be susceptible.

+5
source

All Articles