Odoo Help

Welcome!

This community 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.

0

Why SQL constraints not working?

By
kaynis
on 12/19/13, 6:35 AM 4,491 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.

7

Ray Carnes

--Ray Carnes--
15717
| 9 7 9
Greater Los Angeles, United States
--Ray Carnes--

Senior Odoo Analyst

OpenERP 6.1, 7.0 and Odoo 8.0, 9.0 (Since 2012)

Completed Functional and Technical Training.

Major Skills:

  • Needs Discovery and Requirements Analysis;

  • Function and Technical Specifications;

  • Project Planning;

  • Prototyping and Proof of concepts;

  • Data migration;

  • Configuration & Customization (UI and modules);

  • Integration - data, business logic and service levels;

  • Training and Knowledge transfer;

  • Go Live support;

  • Help desk;

  • Version Migration.

I have over 20 years of experience empowering and enabling users with enterprise information systems that make a real and measurable difference in their ability to proactively manage their businesses and organizations. 

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

--René Schuster--
1560
| 5 5 8
Weinheim, Germany
--René Schuster--

Challenge Everything!

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

Your Answer

Please try to give a substantial answer. If you wanted to comment on the question or answer, just use the commenting tool. Please remember that you can always revise your answers - no need to answer the same question twice. Also, please don't forget to vote - it really helps to select the best questions and answers!

About This Community

This community is for professionals and enthusiasts of our products and services. Read Guidelines

Question tools

0 follower(s)

Stats

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