Skip to Content
เมนู
คุณต้องลงทะเบียนเพื่อโต้ตอบกับคอมมูนิตี้
คำถามนี้ถูกตั้งค่าสถานะ
4 ตอบกลับ
9505 มุมมอง

Hi,

How to make unique Sql constraint for a field on a exisitng database ( because it work for a new database but not for my exisitng db) 


Regards

อวตาร
ละทิ้ง

You mean Existing record?

ผู้เขียน

No for new records

คำตอบที่ดีที่สุด



code = fields.Char(string='Reference', index=True, tracking=True)



_sql_constraints = [('unique_code', 'unique(code)', 'La référence doit être unique !')


Thanks

อวตาร
ละทิ้ง
คำตอบที่ดีที่สุด

Hi,

SQL constrains will work for new and existing db. In existing database, you may have some records which already violates this constrains and thus it is not working.

So manually fix the existing violated records and apply the SQL constrains. In case if you can't fix existing records, then you have to use api.constrains for new records.


Thanks

อวตาร
ละทิ้ง
ผู้เขียน คำตอบที่ดีที่สุด

Hi,

finally it work in my existing db, So the solution is to search for any duplicating data for the field that we want to applied to sql_constraint
remove all duplicate data

remove compiled file "model.pyc"

restart odoo-service

Refresh your browser

and it will work fine

อวตาร
ละทิ้ง
คำตอบที่ดีที่สุด

This is Help for You,

Syntax,

_sql_constraints = [('<sql_constraints_id>', 'unique(<Your field name>)', '<Custom Message>')]

Example,

_sql_constraints = [('user_unique', 'unique(userid)', 'User already exists.')]

อวตาร
ละทิ้ง
ผู้เขียน

Hi,

Thanks for ur reply, this code work just with a new database, for me l have already an exisitng instance in odoo 10 that contiant some records, i want to add this sql_constraints for an exiting field but il will be applied just for the new record ( but this is not work it show nothing).

Hope that u understand the case

Regards