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

Hi im kiruba. Im new in odoo 9. I need how to ssql constraints are using in odoo. Let know any solution ur side

อวตาร
ละทิ้ง
คำตอบที่ดีที่สุด
Hi Kirubanidhi,
SQL constraints are used to specify rules for the data in a table.
If there is any violation between the constraint and the data action, the action is aborted by the constraint.
You can create constrain in your defined object as below:
_sql_constraints
list of (name, sql_definition, message) triples defining SQL constraints to execute when generating the backing table
For more reference check:https://www.odoo.com/documentation/8.0/reference/orm.html#module-openerp.api
Regards,
Kalpana
อวตาร
ละทิ้ง
คำตอบที่ดีที่สุด

course name  all ready exits

_sql_constraints = [ ('course_name',

                                     'UNIQUE (course_name)',

                                    'Course all already exists'), ]

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

This code is for identify unique student id.If any person write duplicate student which is already in the database it gives an error like "Student ID is already exists...!"

_sql_constraints = [

                 ('stud_id_unique', 'unique (stud_id)', 'Student ID is already exists...!')

]


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

_sql_constraints = [

('email_uniq', 'unique(email)', 'Email id is unique change your custom email id'),

]



@api.constrains('email')

def validate_email(self):

for obj in self:

if re.match("^.+\\@(\\[?)[a-zA-Z0-9\\-\\.]+\\.([a-zA-Z]{2,3}|[0-9]{1,3})(\\]?)$", obj.email) == None:

raise ValidationError("Please Provide valid Email Address: %s" % obj.email)

return True



Both constraints are working fine in my module.

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

you can use @api.constrains too

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

Hi,

You may refer any addon in Odoo 9.0. 

For eg, in account module, goto account >> models >> account.py

_sql_constraints = [

('code_company_uniq', 'unique (code,company_id)', 'The code of the account must be unique per company !')

]

You can refer the usage.

อวตาร
ละทิ้ง