Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

E0037 check-constraint-violation

Literal value that violates a CHECK constraint in INSERT ... VALUES or UPDATE ... SET.

CodeNameCategoryDefault
E0037check-constraint-violationcorrectnesserror

What it reports

An INSERT ... VALUES or UPDATE ... SET statement where every column a CHECK constraint reads is assigned a literal or constant expression in the same row, and evaluating the constraint definitely yields false.

In SQL, a CHECK constraint passes if its condition evaluates to TRUE or NULL (unknown); it rejects the row only when it evaluates to FALSE. In PostgreSQL, SQLite, and MySQL (since 8.0.16), writing a literal that violates a CHECK constraint is rejected at runtime by the database engine.

Example

Given a table with CHECK constraints:

CREATE TABLE users (
  id INT PRIMARY KEY,
  age INT CONSTRAINT users_age_check CHECK (age >= 0),
  status TEXT CONSTRAINT users_status_check CHECK (status IN ('active', 'banned'))
);

An invalid insert:

INSERT INTO users (id, age) VALUES (1, -1);
error[E0037]: Value violates check constraint 'users_age_check' for table 'users'
  --> q.sql:1:36
    |
  1 | INSERT INTO users (id, age) VALUES (1, -1);
    |                                    ^^
    = help: Ensure assigned values satisfy 'users_age_check' (age >= 0)

Or an invalid update:

UPDATE users SET status = 'actve' WHERE id = 1;
error[E0037]: Value violates check constraint 'users_status_check' for table 'users'
  --> q.sql:1:27
    |
  1 | UPDATE users SET status = 'actve' WHERE id = 1;
    |                           ^^^^^^^
    = help: Ensure assigned values satisfy 'users_status_check' (status IN ('active', 'banned'))

When it is not reported

Nothing is reported when:

  • A column in the CHECK constraint is assigned a non-constant expression (such as NOW(), another column, or a query parameter), because its runtime value cannot be statically determined.
  • Not all columns referenced by a multi-column CHECK constraint are assigned in the statement (for instance, an UPDATE that changes only one of two columns compared by CHECK (lo <= hi)).
  • The CHECK constraint evaluates to NULL (for example, inserting NULL into a column with CHECK (age >= 0)). In SQL, NULL satisfies CHECK constraints.
  • Any referenced column is not a plain integer, numeric/decimal, or text/varchar column (e.g. date, timestamp, enum, boolean, floating-point, or untyped columns where engine coercion and collation semantics vary), or has an explicit COLLATE clause.
  • For string columns, comparisons other than =, <>, and IN, or string comparisons in MySQL (where default collations are case-insensitive).
  • Numeric literals whose fractional scale exceeds the column's scale (which databases round).
  • The constraint is marked NOT ENFORCED or was dropped with ALTER TABLE ... DROP CONSTRAINT.
  • Running in dialects that treat CHECK constraints as informational or unenforced (such as Snowflake or Redshift).

How to fix it

Provide literal values that satisfy the condition:

INSERT INTO users (id, age) VALUES (1, 18);
UPDATE users SET status = 'active' WHERE id = 1;

To silence for an individual statement:

-- sqlsift:disable check-constraint-violation
INSERT INTO users (id, age) VALUES (1, -1);

Or in sqlsift.toml:

[rules]
check-constraint-violation = "off"