82. Sql Constraints In Odoo || Constrains In Odoo || Odoo 15 Field Validations

แชร์
ฝัง
  • เผยแพร่เมื่อ 31 ม.ค. 2025

ความคิดเห็น • 29

  • @Khmernews-24
    @Khmernews-24 10 หลายเดือนก่อน +1

    Perfect. 🤩

    • @OdooMates
      @OdooMates  10 หลายเดือนก่อน

      thanks

  • @wildbeauty9257
    @wildbeauty9257 2 ปีที่แล้ว

    Thank you. You are doing great work. 👍👍👍

    • @OdooMates
      @OdooMates  2 ปีที่แล้ว

      Most welcome 😊

  • @OleksiyStepaniuk
    @OleksiyStepaniuk ปีที่แล้ว

    Great explanation. Thank you!

    • @OdooMates
      @OdooMates  ปีที่แล้ว +1

      Glad it was helpful!

  • @egyfirst
    @egyfirst ปีที่แล้ว +1

    Perfect. Thank you

    • @OdooMates
      @OdooMates  ปีที่แล้ว

      You're welcome!

  • @danakaplan7738
    @danakaplan7738 ปีที่แล้ว

    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

  • @muthaiyanselvam4047
    @muthaiyanselvam4047 2 ปีที่แล้ว +1

    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......')]

    • @OdooMates
      @OdooMates  2 ปีที่แล้ว +1

      May be there is existing duplicated names in the db ?

    • @OdooMates
      @OdooMates  2 ปีที่แล้ว

      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

    • @muthaiyanselvam4047
      @muthaiyanselvam4047 2 ปีที่แล้ว

      @@OdooMates you are right yes i already fixed in the fields into data files ......., now its working thanks

    • @futhedude4848
      @futhedude4848 2 ปีที่แล้ว +1

      @@OdooMates sir, if there's none values which violates the constrains? How to resolve this?

    • @OdooMates
      @OdooMates  2 ปีที่แล้ว +1

      Fix the existing data manually or use model constrains instead of sql constrains

  • @mrss7306
    @mrss7306 3 หลายเดือนก่อน

    how i can use it for date ?

  • @kenzasouabni
    @kenzasouabni 2 ปีที่แล้ว +1

    after doing this it's always related by id per default not by the field that i need , why?

    • @OdooMates
      @OdooMates  2 ปีที่แล้ว

      can you elaborate with your code

    • @kenzasouabni
      @kenzasouabni 2 ปีที่แล้ว

      @@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 !')]

    • @kenzasouabni
      @kenzasouabni 2 ปีที่แล้ว

      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
      @OdooMates  2 ปีที่แล้ว

      @@kenzasouabni so if you enter duplicated value in the champ1 field, warning is not raised ?

    • @kenzasouabni
      @kenzasouabni 2 ปีที่แล้ว

      @@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

  • @eventdz6182
    @eventdz6182 2 ปีที่แล้ว +1

    it's not working in odoo 15

    • @OdooMates
      @OdooMates  2 ปีที่แล้ว +1

      This will work in all versions, may be there is already some record that violating this constrains?

  • @futhedude4848
    @futhedude4848 2 ปีที่แล้ว +1

    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.

    • @OdooMates
      @OdooMates  2 ปีที่แล้ว +1

      the advantages sql constrains is,it is much faster than api constrains

    • @futhedude4848
      @futhedude4848 2 ปีที่แล้ว +1

      @@OdooMates thanks you.