Both sql and python limits do not work in odoo 9

I have been struggling with this for a while. neither of the two works and they give no errors. I have commented out the pythonic constrain method for you.

snippet of code:

class house_development(models.Model):
    _name = 'house.development'
    _description = 'Development'
    _inherit = ["mail.thread"]

    name = fields.Char(string="Name", required=True, track_visibility='onchange')
    description = fields.Text(string="Description", track_visibility='onchange')

    # @api.one
    # @api.constrains('name')
    # def _identify_same_name(self):
    #     for record in self:
    #         if record.name in self:
    #             raise exceptions.ValidationError("There is another development/project with the same name: %s" % record.name)

    _sql_constraints = [
        ('name_unique',
         'UNIQUE(name)',
         "There is another development/project with the same name"),
    ] 

      

+3


source to share


1 answer


It should be like this

@api.multi
@api.constrains('name')
def _identify_same_name(self):
    for record in self:
        obj = self.search([('name','=ilike',record.name),('id','!=',record.id)])
        if obj:
            raise exceptions.ValidationError("There is another development/project with the same name: %s" % record.name)

      

You need to find the same name but not with the same ID.

And for unique database constraints, you can add this.



_sql_constraints = [
    ('name_unique', 'unique(name)', 'There is another development/project with the same name!!!'),
] 

      

Database constraints will not be added if there is a duplicate name in the table. Sql limits will only apply if the rule does not violate existing data. I think in your case the point with Sql is holding back. And make sure you need to update the module. first check for duplicate records in the database by enabling this query.

Select name, count(*) from table_name 
group by name
having count(*) > 1;

      

Available operators per domain in odoo Click to see more .

+2


source







All Articles