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- Categoria
- Constraint Error
O que 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 comuns
- 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;
Como corrigir
-
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.
Reproduza com segurança
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))
Perguntas
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.
Erros relacionados
Constraint ErrorA NOT NULL column received NULLConstraint ErrorA duplicate key violates a unique constraint23514PostgreSQL: violação de restrição de verificaçãoSQLITE_CONSTRAINTSQLite: falha na restrição
O código e o texto da mensagem são de DuckDB documentation. Veja toda a DuckDB error list ou pesquise todos os erros do banco de dados.
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 e dados desta página: CC BY 4.0, com crédito ao SQLly (sqlly.app). Código e trechos SQL: MIT. Detalhes da licença O texto citado das mensagens de erro pertence ao fornecedor do banco de dados.