A note for everyone trying this and not worked at the first time check data files for duplicated data, then the data in your module, delete duplicated ones then restart the service and upgrade the module
Hiiiiiii....Odoomates.... i'm regularly following your videos...thanks for your past & future videos........ currently i'm using the odoo14 version..... i tried this sql_constraints............not working in my system............ please advice me class HospitalToggles(models.Model): _name = "patient.toggle" _description = 'Hospital Appointment' name = fields.Char(string='Name') _sql_constraints = [('unique_toggle_name', 'unique(name)', 'name is unique......')]
and the class A is like this _name = "module.A" _description = "A" _rec_name = "champ1" champ1 = fields.Integer(string="champ1", required=True) _sql_constraints = [('unique_name', 'unique(champ1)', 'champ1 must be unique !')]
@@OdooMates The error "NS must be unique" raised but when I want to see the tables in the command prompt, field1 of table A is correct but field1 of table dd is not the same as table A it returns me the value of the id in field1 and not the value of field1
this _sql_constraints have a huge weakness that if there's already some records which have values violates the rules, then that rule is not affected. in that case, we should use @api.constrains instead. in fact, this is not good to use, i recommend to use @api.constrains instead of _sql_constraints in every posible way.
Perfect. 🤩
thanks
Thank you. You are doing great work. 👍👍👍
Most welcome 😊
Great explanation. Thank you!
Glad it was helpful!
Perfect. Thank you
You're welcome!
A note for everyone trying this and not worked at the first time
check data files for duplicated data, then the data in your module, delete duplicated ones then restart the service and upgrade the module
Hiiiiiii....Odoomates.... i'm regularly following your videos...thanks for your past & future videos........ currently i'm using the odoo14 version..... i tried this sql_constraints............not working in my system............
please advice me
class HospitalToggles(models.Model):
_name = "patient.toggle"
_description = 'Hospital Appointment'
name = fields.Char(string='Name')
_sql_constraints = [('unique_toggle_name', 'unique(name)', 'name is unique......')]
May be there is existing duplicated names in the db ?
If there is already values that violates the constrains it will not work, try deleting or updating those values manually and try or try in a fresh db
@@OdooMates you are right yes i already fixed in the fields into data files ......., now its working thanks
@@OdooMates sir, if there's none values which violates the constrains? How to resolve this?
Fix the existing data manually or use model constrains instead of sql constrains
how i can use it for date ?
after doing this it's always related by id per default not by the field that i need , why?
can you elaborate with your code
@@OdooMates yeah sure
_name = "module.dd"
_description = "Dd"
_inherits = {'module.A': 'champ1'}
_rec_name = 'champ2'
champ1 = fields.Many2one('module.A', required=True, ondelete='restrict', auto_join=True, string="Related champ1")
champ2= fields.Char(string="champ2")
_sql_constraints = [('unique_champ_name', 'unique(champ1)', 'champ1 must be unique !')]
and the class A is like this
_name = "module.A"
_description = "A"
_rec_name = "champ1"
champ1 = fields.Integer(string="champ1", required=True)
_sql_constraints = [('unique_name', 'unique(champ1)', 'champ1 must be unique !')]
@@kenzasouabni so if you enter duplicated value in the champ1 field, warning is not raised ?
@@OdooMates The error "NS must be unique" raised but when I want to see the tables in the command prompt, field1 of table A is correct but field1 of table dd is not the same as table A it returns me the value of the id in field1 and not the value of field1
it's not working in odoo 15
This will work in all versions, may be there is already some record that violating this constrains?
this _sql_constraints have a huge weakness that if there's already some records which have values violates the rules, then that rule is not affected.
in that case, we should use @api.constrains instead.
in fact, this is not good to use, i recommend to use @api.constrains instead of _sql_constraints in every posible way.
the advantages sql constrains is,it is much faster than api constrains
@@OdooMates thanks you.