Grf
Grf

Reputation: 65

Odoo10 api constrains stuck

My constrains method not working for id_number. can't figure it out why.

from odoo import models, fields, api
from odoo.exceptions import ValidationError

class KindeGarden(models.Model):
    _inherits = {'res.partner': 'partner_id'}
    _name = 'kindergarten.model'
    _description = 'Kindergarten'

   age = fields.Integer(string="Amžius", required=False, default="1")
   group = fields.Char(string="Grupė", compute="_compute_group", store=True)
   height = fields.Float(string="Ūgis", required=False)
   weight = fields.Float(string="Svoris",  required=False)
   id_number = fields.Integer(string="Registravimo Nr", required=True)

   @api.constrains('id_number')
   def _check_id_number_field(self):
       for i in self:
           if i.id_number < 10:
               raise ValidationError("Number is to small")

and i'm also having this

WARNING -Kindegarden odoo.models.schema: Table 'kindergarten_model': unable to set a NOT NULL constraint on column 'id_number' ! If you want to have it, you should update the records and execute manually: ALTER TABLE kindergarten_model ALTER COLUMN id_number SET NOT NULL

Upvotes: 1

Views: 820

Answers (2)

leppy
leppy

Reputation: 99

Like mentioned above, it looks like it is some data are null already before you set required parameter to true.

odoo has a shell you can use to access your DB if you are not familiar with SQL.

odoo-bin -d <database_name> shell

inside the shell, do as follow so you will see.

>> records = env['kindergarten.model'].search([('id_number','=',False)])
>> len(records)

if it returns a number aside from 0, it means that those are NULL value. so do like.

>> for record in records:
       record.write({'id_number': 0.0})
>>env.cr.commit()

Then update your module again.

If this doesn't work you will need to do it manually with SQL.

Upvotes: 1

Dreambits Tech
Dreambits Tech

Reputation: 16

Did you add constraint after few records were added ?

The error you got generally comes when postgres is unable to set "NOT NULL" to the column because it already has null values

Upvotes: 0

Related Questions