Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the correct format to use Unique in _sql_constraints in OpenERP?

I have tried unique in sql_constaints in OpenERP(Odoo) using two different methods using flower brackets {} or square brackets []. Both works fine. 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.')
    ]

P.S: But if I want to use more than a constraint it accepting only 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 behind it?

like image 743
Tintumon M Avatar asked Jul 20 '15 09:07

Tintumon M


2 Answers

The correct one is square brackets syntax.

  1. you can grep on _sql_constraints and see it is what is always used,

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

    _sql_constraints = []
    #...
        cls._local_sql_constraints = cls.__dict__.get('_sql_constraints', [])
  1. in Odoo 8.0 documentation it is said:

list of (name, sql_definition, message) triples defining SQL constraints to execute when generating the backing table.

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

The syntax {} either creates:

  • a dictionary if it is empty {} or if there is keys values like this: {'keyA': 'valueA', 'keyB': 'valueB'},
  • as of python 2.7 a set if instantiated like this: {'value1', 'valueB', 42}
like image 161
ea- Avatar answered Sep 19 '22 19:09

ea-


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

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

you can give more then one sql constraint in list of tuples.

when you define a dictionary with out key & value python treat it as set. and set can be itrable.

like image 25
Atul Arvind Avatar answered Sep 20 '22 19:09

Atul Arvind