Skip to content

OD110 sql-constraints-unsupported

_sql_constraints is ignored; declare models.Constraint attributes.

Category Severity Series Needs the index
Deprecation Error 19.0 and later no

What it reports

A model class that assigns _sql_constraints, in a module of series 19.0 or later.

Why it matters

From 19.0, Odoo reads table constraints and indexes from class attributes built with models.Constraint, models.Index and models.UniqueIndex, and no longer reads _sql_constraints. When it loads such a model, it logs a warning and creates none of the constraints listed there, so a database that does not have them yet accepts the duplicates or invalid values they were meant to refuse, and the data stays wrong after the mistake is found.

Modules of earlier series are not checked, since _sql_constraints is the right attribute there. The attribute name must start with an underscore: _isbn_uniq names the constraint library_book_isbn_uniq in the database, as the isbn_uniq entry it replaces did.

Example

models/library_book.py
class LibraryBook(models.Model):
    _name = "library.book"
    _description = "Library book"

    isbn = fields.Char(required=True)

    _sql_constraints = [("isbn_uniq", "UNIQUE(isbn)", "A book with this ISBN already exists.")]

Odoo 19.0 ignores the list, so two books can share an ISBN.

models/library_book.py
class LibraryBook(models.Model):
    _name = "library.book"
    _description = "Library book"

    isbn = fields.Char(required=True)

    _isbn_uniq = models.Constraint("UNIQUE(isbn)", "A book with this ISBN already exists.")

Suppressing

_sql_constraints = []  # odooscan: ignore[OD110]

See suppressing a finding.