CHECK constraint failed
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.
- 1Inserting a value that violates an inline column CHECK expression (e.g., CHECK(price > 0))
- 2Inserting a row that violates a table-level CHECK constraint involving multiple columns
A negative price is inserted into a column with a CHECK(price > 0) constraint.
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 violationexpected output
sqlite3.IntegrityError: CHECK constraint failed: products
Fix
Validate data before inserting
WHEN When data comes from external sources and may violate business rules.
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) # okWhy 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.
✕ Remove the CHECK constraint to make the insert succeed
CHECK constraints encode business invariants. Removing them allows logically invalid data (e.g., negative prices) into the database, causing downstream calculation errors.
Extended code 275 introduced. Earlier versions return base SQLITE_CONSTRAINT.
Content generated with AI assistance and reviewed for accuracy. Found an error? hello@errcodes.dev