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

How can i add Unique constriant to field in openerp?

By
samba
on 7/31/14, 2:43 AM 3,835 views

provide us your code in detail in description itself

Atchuthan - Technical Consultant, Sodexis Inc
on 7/31/14, 6:26 AM
1

Prakash

--Prakash--
3173
| 5 4 7
Bikaner, India
--Prakash--

OpenERP Devloper at SunARC Technology

Prakash
On 7/31/14, 2:47 AM

In python to add,

   _sql_constraints = [
        ('name_uniq', 'unique (name)', 'The name must be unique !')
    ]

 

I have written like this it's not working class product_extension(osv.osv): _inherit = "product.product" _columns = { 'product_code_uniq':fields.char('SKU/Product Code', size=64, help="Enter Code", required=True), } _sql_constraints = [ ('product_code_uniq', 'unique(product_code_uniq)', 'Product Code must be unique!'), ]

samba
on 7/31/14, 3:23 AM

Check the product.product form or table if already unique present in the table then remove the records. After that Restart the server and update the module.

Prakash
on 7/31/14, 3:27 AM

I think field name is "product_code" so your_sql_constraints = [ ('product_code_uniq', 'unique(product_code)', 'Product Code must be unique!'), ]

Prakash
on 7/31/14, 3:29 AM

Thank you prakash, to spent your valuable time with me. But this also not working. I have restatrt server and upgrade my module every time when i do any code changes. _inherit = "product.product" _columns = { 'product_code':fields.char('SKU/Product Code', size=64, help="Enter Code", required=True), } _sql_constraints = [ ('product_code_uniq', 'unique(product_code)', 'Product Code must be unique!'), ]

samba
on 7/31/14, 5:33 AM

Please make sure 'product_code' column in available in product.product table?. Default openerp 'default_code' code is available. Refer the link https://www.odoo.com/forum/help-1/question/why-sql-constraints-not-working-39549

Prakash
on 7/31/14, 5:59 AM

previcously product_code not available. I want to add product_code. After my module installation the field is available in product_product table. Please check my .py file from openerp.osv import fields, osv import os.path import fnmatch from tools.translate import _ class product_extension(osv.osv): _inherit = "product.product" _columns = { 'product_code':fields.char('SKU/Product Code', size=64, help="Enter Code", required=True), } _sql_constraints = [ ('product_code_uniq', 'unique(product_code)', 'Product Code must be unique!'), ] and .xml file product.normal.form.view.extension.formproduct.productproduct.normal.tree.view.extension.formproduct.product

samba
on 7/31/14, 6:12 AM

using DB pg_admin you can easily check Column and Constraints are created Steps 1)Select the table you want 2) Ctrl-Alt-Enter or right-click / Properties 3) Select "Columns" tab 4) Select "Constraints" tab otherwise issues in the custom module.

Prakash
on 7/31/14, 6:24 AM

Thank you prakash, I got the problem. I have removed all the products which have the value of "product_code" field. I have started working with new product, then this constraint working...

samba
on 7/31/14, 6:26 AM
2

Jagdish Panchal

--Jagdish Panchal--
2674
| 5 3 6
Gandhinagar, India
--Jagdish Panchal--

OpenERP Developer

Jagdish Panchal
On 7/31/14, 2:48 AM

Hi

Add this code in py file

    _sql_constraints = [
        ('your_field_uniq', 'unique(your_field)', 'your_field must be unique!'),
    ]

I have written like this it's not working class product_extension(osv.osv): _inherit = "product.product" _columns = { 'product_code_uniq':fields.char('SKU/Product Code', size=64, help="Enter Code", required=True), } _sql_constraints = [ ('product_code_uniq', 'unique(product_code_uniq)', 'Product Code must be unique!'), ]

samba
on 7/31/14, 3:23 AM

Hi try this _inherit = "product.product" _columns = { 'product_code':fields.char('SKU/Product Code', size=64, help="Enter Code", required=True), } _sql_constraints = [ ('product_code_uniq', 'unique(product_code)', 'Product Code must be unique!'), ]

Jagdish Panchal
on 7/31/14, 3:34 AM

upgrade your module, if there is value in this field is duplicate in old product then it not apply

Jagdish Panchal
on 7/31/14, 3:35 AM

Thank you jagdish, to spent your valuable time with me. But this also not working. I have restatrt server and upgrade my module every time when i do any code changes.

samba
on 7/31/14, 5:30 AM

Thank you jagadish, I got the problem. I have removed all the products which have the value of "product_code" field. I have started working with new product, then this constraint working...

samba
on 7/31/14, 6:27 AM
0
jonathan hartley
On 1/27/15, 9:50 AM

Where the heck is "_sql_constraints" documented? (I'd add this as a comment, but the stoopid karma system won't let me.)

0
Mariusz Mizgier
On 7/31/14, 6:53 AM

samba - if you have such issue, when column is populated with data, you should add that sql_constraint into your code, but as well you should try to get this column having distinct values, then manually try to set a sql constraint on your database - that will work for sure. Just remember to make sure that column has no duplicate value and then create a key.

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

2 follower(s)

Stats

Asked: 7/31/14, 2:43 AM
Seen: 3835 times
Last updated: 3/16/15, 8:10 AM