Odoo Help


Why SQL constraints not working?

on 12/19/13, 6:35 AM 6,868 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

Ray Carnes

--Ray Carnes--
| 9 7 9
Keyport, United States
--Ray Carnes--

Senior ERP Analyst

OpenERP 6.1, 7.0; Odoo 8.0, 9.0, 10.0 and 11.0.

Completed Odoo Functional and Technical Training.

Functional Areas:

  • CRM/Sales

  • Inventory

  • Manufacturing

  • Accounting

  • Purchasing

Major Skills:

  • Business Process Reengineering

  • Efficiency Consulting

  • User Needs and GAP Analysis;

  • Functional and Technical Design;

  • Prototyping and Proof of Concepts;

  • Requirements Specifications;

  • Agile Implementation;

  • Data Migration;

  • Configuration & Customization (UI and code);

  • Integration - Odoo and non Odoo Applications and Services;

  • Training and Knowledge Transfer;

  • Go Live Support;

  • Helpdesk;

  • Version Upgrades and Migration.

  • Accounting Expert.

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. 

I am a skilled Communicator and I only take the serious things seriously!

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?

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

on 12/20/13, 6:05 AM

René Schuster

--René Schuster--
| 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

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.

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

on 12/19/13, 8:46 AM

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

on 12/19/13, 8:53 AM
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.


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)


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