Constraint Error: A CHECK constraint rejected the row
An INSERT or UPDATE produced a row that failed a CHECK expression declared on the table.
- Exception
Constraint Error- Categoría
- Constraint Error
Qué significa
A CHECK constraint is an expression evaluated per row on INSERT and UPDATE. When it comes out false, DuckDB rejects the row and the whole statement fails: in a multi-row insert nothing lands. NULL passes, as in standard SQL, because a CHECK only rejects rows where the expression evaluates false.
The message shows the table and the exact expression, which is usually enough to see what was refused. When the expression itself throws instead of returning false, for example a cast inside the CHECK, recent DuckDB versions append the inner error as (Error: ...), so read that part too.
Causas comunes
- An INSERT or UPDATE with a value the expression refuses, such as a negative quantity or a date before a lower bound.
- A default that itself violates the CHECK, so omitting the column fails the insert.
- A COPY or INSERT INTO ... SELECT where some source rows fail the expression even though the statement as a whole looks correct.
- An UPDATE that changes one column of a multi-column CHECK and leaves the row inconsistent.
Find the cause
-- The CHECK expressions declared on the table: SELECT constraint_type, constraint_text FROM duckdb_constraints() WHERE table_name = 'orders'; -- The columns the expression touches: DESCRIBE orders;
Cómo solucionarlo
-
List the rows that would fail before inserting. NULLs pass, so the check excludes them:
SELECT * FROM stage WHERE quantity IS NOT NULL AND NOT (quantity > 0);
-
Repair or filter the source, then insert:
INSERT INTO orders SELECT * FROM stage WHERE quantity > 0;
-
DuckDB cannot add or drop a CHECK with ALTER TABLE, so a wrong constraint means rebuilding the table:
CREATE TABLE orders_new (quantity INTEGER CHECK (quantity > 0)); INSERT INTO orders_new SELECT * FROM orders; ALTER TABLE orders RENAME TO orders_old; ALTER TABLE orders_new RENAME TO orders;
Other ways to handle it
Where the rule is really a data-quality check rather than an invariant, validate at load time and report rejects instead, since DuckDB cannot alter a CHECK on an existing table. Keep CHECKs for invariants that must never break, such as positive quantities or an end date after the start date.
Reprodúcelo de forma segura
CREATE TABLE t(a INT CHECK (a > 0)); INSERT INTO t VALUES (0); -- Constraint Error: CHECK constraint failed on table "t" with expression CHECK((a > 0))
Preguntas
Does NULL pass a CHECK constraint?
Yes. A CHECK rejects only rows where the expression evaluates false. To forbid NULL as well, declare the column NOT NULL.
Which row failed?
The message names the constraint, not the row, and the statement fails as a whole. Run the insert as a SELECT with NOT (expression) against the source to list the offending rows.
Errores relacionados
Constraint ErrorA NOT NULL column received NULLConstraint ErrorA duplicate key violates a unique constraint23514PostgreSQL: violación de restricción de comprobaciónSQLITE_CONSTRAINTSQLite: falló la restricción
El código y el texto del mensaje son de DuckDB documentation. Explora la lista completa de DuckDB error list o buscar cada error de base de datos.
Work on DuckDB in SQLly
A native SQL client for macOS, Windows and Linux with schema-aware IntelliSense, guardrails for risky statements and clear error messages, for DuckDB and the other databases you use.
Texto y datos de esta página: CC BY 4.0, con atribución a SQLly (sqlly.app). Código y fragmentos SQL: MIT. Detalles de la licencia El texto citado de los mensajes de error pertenece a su proveedor de base de datos.