コンテンツへスキップ
メニュー
この質問にフラグが付けられました
4452 ビュー

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






アバター
破棄
関連投稿 返信 ビュー 活動
2
3月 15
10686
3
11月 22
5870
1
6月 17
1282
2
11月 15
6876
2
9月 21
3754