python38292
python38292

Reputation: 69

SQL Constraints are not running in Odoo 11

Problem

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:

Solution

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.

Upvotes: 0

Views: 1352

Answers (2)

Ro Ma
Ro Ma

Reputation: 59

another possible solution to avoid change the fields names, "" the field name, I tested in "unique" case

_sql_constraints = [('date_constraint', 'CHECK(("endDate" > "startDate"))', 'End date must be later than start date')]

Upvotes: 0

Yajo
Yajo

Reputation: 6418

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.

Upvotes: 2

Related Questions