pgref.dev/sqlite/errors/SQLITE_CONSTRAINT_CHECK
SQLITE_CONSTRAINT_CHECKERRORTier 1 — Safe✅ HIGH confidence

CHECK constraint failed

Category: Constraint ViolationVersions: SQLite 3.7.16+

What this means

SQLITE_CONSTRAINT_CHECK (extended code 275) is raised when an inserted or updated value fails a CHECK constraint expression defined on the table or column. SQLite evaluates CHECK expressions as SQL expressions — any expression that returns FALSE or a value that is not truthy causes this error.

Why it happens

  1. 1Inserting a value that violates an inline column CHECK expression (e.g., CHECK(price > 0))
  2. 2Inserting a row that violates a table-level CHECK constraint involving multiple columns

How to reproduce

A negative price is inserted into a column with a CHECK(price > 0) constraint.

trigger — this will ERROR
import sqlite3
conn = sqlite3.connect(':memory:')
conn.execute('CREATE TABLE products (id INTEGER PRIMARY KEY, price REAL CHECK(price > 0))')
conn.execute('INSERT INTO products VALUES (1, -5.0)')  # triggers CHECK violation
sqlite3.IntegrityError: CHECK constraint failed: products

Fix 1: Validate data before inserting

When data comes from external sources and may violate business rules.

fix
import sqlite3

def insert_product(conn, product_id, price):
    if price <= 0:
        raise ValueError(f"Price must be positive, got {price}")
    conn.execute('INSERT INTO products VALUES (?, ?)', (product_id, price))

conn = sqlite3.connect(':memory:')
conn.execute('CREATE TABLE products (id INTEGER PRIMARY KEY, price REAL CHECK(price > 0))')
insert_product(conn, 1, 9.99)  # ok

Why this works

Application-level validation before the INSERT produces a clear, domain-specific error message rather than a generic SQLite constraint error. This also allows more complex validation logic than a SQL CHECK expression can express.

What not to do

Remove the CHECK constraint to make the insert succeed

Why it's wrong: CHECK constraints encode business invariants. Removing them allows logically invalid data (e.g., negative prices) into the database, causing downstream calculation errors.

Version notes

SQLite 3.7.16+Extended code 275 introduced. Earlier versions return base SQLITE_CONSTRAINT.

Sources

📚 Official docs: https://www.sqlite.org/rescode.html#constraint_check

🔧 Source ref: sqlite3.h — SQLITE_CONSTRAINT_CHECK = 275

📖 Further reading: SQLite CHECK constraints

Confidence assessment

✅ HIGH confidence

Stable. CHECK constraint semantics are well-defined.

See also

📄 Reference pages

CHECK constraintSQLite CREATE TABLE
⚙️ This error reference was generated with AI assistance and reviewed for accuracy. Examples are provided to illustrate common scenarios and may not cover every case. Always test fixes in a development environment before applying to production. Spotted an error? Suggest a correction →