Skip to content

deprecated-sql-constraints (ODE9501)

Preview (since 0.16.3.33) · Related issues · View source

Derived from the odoo linter.

Fix is sometimes available.

This rule is unstable and in preview. The --preview flag is required for use.

What it does

Checks for the _sql_constraints attribute on an Odoo model class.

Why is this bad?

Odoo 19.0 dropped the attribute in favor of models.Constraint descriptors. Loading a model that still carries it only logs Model attribute '_sql_constraints' is no longer supported, please define models.Constraint on the model. and carries on, so the module installs while every constraint it declared silently stops being created: the database loses the uniqueness and check rules it used to enforce, and nothing fails until the data they were guarding against shows up.

Example

class ResPartnerCategory(models.Model):
    _name = "res.partner.category"

    _sql_constraints = [
        ("name_uniq", "unique (name)", "The name must be unique!"),
    ]

Use instead:

class ResPartnerCategory(models.Model):
    _name = "res.partner.category"

    _name_uniq = models.Constraint("unique (name)", "The name must be unique!")

Options

Fix safety

The constraint keeps its identity in the database: both APIs name it {table}_{key}, and the key is what the attribute is called minus its leading underscore, so ("name_uniq", ...) has to become _name_uniq for the database to see the same constraint it already has. The fix is therefore a rename on the Python side only, with no migration script to write.

It is marked as unsafe unless odoo-version is set to 19.0 or later, because models.Constraint does not exist before 19.0: on an older Odoo the rewritten model raises AttributeError at import. With the version configured, the rewrite is behavior-preserving and the fix is safe.

No fix is offered when the rewrite cannot be done by moving source around: a value that is not a list of (key, definition[, message]) tuples, a key that is not an identifier or already starts with _ (which Python would mangle), a key the class already binds, a duplicated key, an entry spread over several lines, or a comment inside the assignment that the rewrite would drop.