I have the following model myModel
:
# -*- coding: utf-8 -*-
from odoo import models, fields, api
class myModel(models.Model):
_name = 'myproject.myModel'
name= fields.Char('Name', size=9, required=True)
startDate = fields.Datetime('Start date',required=True)
endDate= fields.Datetime('End date',required=True)
_sql_constraints = [('date_constraint', 'CHECK((endDate > startDate))', 'End date must be later than start date')]
The problem is that the sql constraints are not making anything. I have tried:
_sql_constraints = [('name_constraint', 'UNIQUE((name))', 'Name must be unique.')]
The problem was that the field contained uppercase and it seems that the sql_constraint converts everything to lowercase, so that field was not found in the database. So I convert startDate to startdate and endDate to enddate.
Welcome! Thanks for posting a good question with all needed details. 😁
You need to update the addon.
If still it doesn't work, check the logs. Odod will fail to install a SQL constraint if any preexisting record violates it. In such case, it will log a WARNING. Delete or fix that record and upgrade the module again.