Skip to Content
Menu
This question has been flagged
1 Reply
560 Views



@api.constrains('phone', 'owner')

    def check_phone_owner_uniq(self):

        for record in self:

            duplicate_records = self.search([

                ('phone', '=', record.phone),

                ('owner', '=', record.owner.id),

                ('id', '!=', record.id)

            ])

            if duplicate_records:

                raise ValidationError("You cannot have two tenants with the same phone number for the same owner!")



I want to use api.constrains to check. It's working, but when debug I check id record and I see its still create record (its only hide this record). 

How can not create record when it show an error message?


Avatar
Discard
Best Answer

Hi,

Try this code:

_sql_constraints = [

        (

            "phone_owner_uniq",

            "unique (phone, owner)",

            "You cannot have two tenants with the same phone number for the same owner!",

        )

    ]



Hope it helps

Avatar
Discard
Author

Thanks for your help. Its working for me