Skip to Content
Menu
This question has been flagged

Hello everyone... once again for today,

I need your help on trying to figure out why this sql constraint does not work for the "name" field on product.template (and also with some other fields, but not all):

_sql_constraints = [
                    ('name_unique','unique(name)', _("Product name must be unique!")),
                    ]

This is a standard Odoo field and tried with some other core and custom fields and the same happens, but not with all fields.

Question arises when it works fine with other fields. For instance:

_sql_constraints = [
                    ('code_unique','unique(default_code)', _("Product code must be unique!")),
                    ]

...works great!!!

For my specific situation I had to use a Python Constrain like:

@api.multi 
@api.constrains('name') 
def _unique_name(self): 
    for record in self: 
    obj = self.search([('name','=ilike',record.name),('id','!=',record.id)]) 
    if obj: 
        raise ValidationError(_("Product name must be unique"))

Can anyone explain me why sql constraint does not work for some of the fields?
I have tested on a database with no data for the products template. I know sql constraints apply to both new and existing data and if there is some existing data that violates the constraint it does not work, but this is not the case.

Than you all in advance

PM






Avatar
Discard
Related Posts Replies Views Activity
2
Mar 15
9379
3
Nov 22
4116
1
Jun 17
1282
2
Nov 15
5719
2
Sep 21
2658