Skip to Content
Meniu
Trebuie să fiți înregistrat pentru a interacționa cu comunitatea.
Această întrebare a fost marcată
2 Răspunsuri
27528 Vizualizări

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.

Imagine profil
Abandonează

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

Autor

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

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

Cel mai bun răspuns

Your code seems right to me.

Have you recompiled your source / updated the db?

Constrains will not work if there is duplicated entries already existing in the db

Imagine profil
Abandonează
Autor

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

And the field containing SKU is named 'default_code' ?

Autor

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

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

Autor

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

Autor

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

I got the same issue : I add a _sql_constraint in my python code and got any constraint more in the database,
and the only 'alert' I got is a tiny error message in the big, big odoo.log file with "ERROR 'my database' odoo.schema: Table 'my table': unable to add constraint 'constraint name in python _sql_constraint' ....
now, I have to understand why odoo is unable to add the constraint ...

Cel mai bun răspuns
Hello! I had the same problem. My sql_constraints (in my case for a unique name) was correct but I was still able to create records with the same name.

Fixed after removing records with duplicate names (I already had them before applying sql_constraints). I did this and it started working correctly. So make sure you don't have any records with the attribute you want to apply duplicate sql_constraints to before implementing sql_constraints, otherwise it won't work.


Imagine profil
Abandonează