Odoo Help

0

Why SQL constraints not working?

By
kaynis
on 12/19/13, 6:35 AM 5,899 views

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.

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

meriem HAKKANI
on 9/15/17, 2:28 AM
8
Ray Carnes
On 12/19/13, 7:40 PM

If there is already data in the system that violates the constraint, it won't work - because it can't be applied.

The constraint has to be for all records - those that already exist, those that will be added via your module, and those that will be added by other modules.

Thanks for responding. There are no duplicated data in the particular database am trying to apply this to but there are in another database in the system. Could this also affect the module working?

kaynis
on 12/20/13, 4:10 AM

i followed the same steps in custom module its working 1) __openerp.__.py file depends "product" Module added 2) class product_product(osv.osv): _inherit = "product.product" product_product() # the same above code

Prakash
on 12/20/13, 6:05 AM
0
René Schuster
On 12/19/13, 7:22 AM

Your code seems right to me.

Have you recompiled your source / updated the db?

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

kaynis
on 12/19/13, 8:09 AM

And the field containing SKU is named 'default_code' ?

René Schuster
on 12/19/13, 8:11 AM

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

kaynis
on 12/19/13, 8:35 AM

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

René Schuster
on 12/19/13, 8:38 AM

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

kaynis
on 12/19/13, 8:46 AM

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

kaynis
on 12/19/13, 8:53 AM
0
kaynis
On 12/24/13, 7:48 AM

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

About This Community

This platform is for beginners and experts willing to share their Odoo knowledge. It's not a forum to discuss ideas, but a knowledge base of questions and their answers.

Register

Odoo Training Center

Access to our E-learning platform and experience all Odoo Apps through learning videos, exercises and Quizz.

Test it now

Question tools

0 follower(s)

Stats

Asked: 12/19/13, 6:35 AM
Seen: 5899 times
Last updated: 3/16/15, 8:10 AM