Help

0

Why SQL constraints not working?

Avatar
kaynis

Working on V6.1 on window.

I have added this constraint on a custom module to force the default_code (Product Reference) to be unique but it does not work i can still add products with similar references.

    'default_code': fields.char('Reference', size=80, select=True, required=True),
}    
_sql_constraints = [
    ('code_uniq', 'unique(default_code)', ("There is a similar SKU/References already in the system please specify another name, SKU must be UNIQUE!"))
]

Any suggestions please. Thank you in advance.

3 Comments
Avatar
Discard
Avatar
meriem HAKKANI
-

you must be sure that there is no duplicate of default code in the database

Avatar
kaynis
-

The code works fine, the problem was as Ray Carnes stated, there was a product i had hidden in the past that shared the same product default code with another product in the database

Avatar
Sehrish
-

You must check check is no duplicate entry in database. If it is , your constraint will not be create.

1 Answer
0
Avatar
René Schuster
Best Answer

Your code seems right to me.

Have you recompiled your source / updated the db?

6 Comments
Avatar
Discard
Avatar
kaynis
-

I know the module is working because all other characteristics are already functioning fine just the constraint

Avatar
René Schuster
-

And the field containing SKU is named 'default_code' ?

Avatar
kaynis
-

Yes see the change about. Plus the text SKU is only appearing in the warning text which i think should not affect the code.

Avatar
René Schuster
-

You know that the sql-constraints will only be testes when the record is saved (create/write method)?

Avatar
kaynis
-

Yeah but i can create and save the same reference more than once and I cannot even add ALTER TABLE product_product ADD CONSTRAINT product_product_default_code_uniq UNIQUE (default_code); in postgresql because another database which does not use this module already have duplicated references

Avatar
kaynis
-

Thanks for your response. Hope you can help solve this issue.