Odoo Help


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.


unique multiple fields

Fongoh Tayong
on 2/1/16, 7:28 AM 535 views

I am customizing accounts module. 

I want to make sure that an item is not added multiple times on an invoice(account.invoice.line). 

From this question, https://www.odoo.com/forum/help-1/question/how-to-make-a-unique-field-in-a-module-16386, i came out with a solutions which does not work and dont know why. 

_name = "account.invoice.line"

_inherit = "account.invoice.line"

#other code

_sql_constraints = [
('unique_product', 'unique(id, product_id)', 'Cannot Use one tracker twice!\nPlease, select a different product')

It still allows me to add same product twice. 
Please, where am i going wrong?

Thanks for your reply


Hardikgiri Goswami

--Hardikgiri Goswami--
| 2 1 5
Rajkot, India
--Hardikgiri Goswami--

OpenERP/Odoo Customization/Integration Expert / Python Programmer

Skype ID : hardikgiri.goswami

Hardikgiri Goswami
On 2/1/16, 9:15 AM

Hi Fongoh,

I think following code will be helpful to you. Please try with this code.

_name = "account.invoice.line"	

_inherit = "account.invoice.line"

#other code
_sql_constraints = [ ('unique_product', 'unique(invoice_id, product_id)', 'Cannot Use one tracker twice!\nPlease, select a different product') ]

Instead of "id", you can try with "invoice_id".



I tried that but not change. I even tried using a field(in the unique constraint) that is not in the model but it did not raise any error/exception of field not found. I thought it would have raised an exception that field not found but was supprised it just went pass that and could still have duplicate product selection.

Fongoh Tayong
on 2/3/16, 6:22 AM

Oh, thanks hardikgiri, it worked. It was actually failing to add because the constraint had already been violated by an entry in the db so the error was being shown on the command line (was expecting a browser error). So as soon as i removed the record that violdated the constraint, and restarted odoo, it added the constraint and now, no duplicate product can be entered. Thanks man. Marked succesfull.

Fongoh Tayong
on 2/11/16, 1:31 AM

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

1 follower(s)


Asked: 2/1/16, 7:28 AM
Seen: 535 times
Last updated: 2/24/16, 2:11 PM