// DuckDB error reference

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
类别
Constraint Error
错误信息
Constraint Error: CHECK constraint failed on table "t" with expression CHECK((a > 0))

它的意思

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.

常见原因

  • 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;

如何修复它

  1. 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);
  2. Repair or filter the source, then insert:

    INSERT INTO orders
    SELECT * FROM stage WHERE quantity > 0;
  3. 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.

安全地复现它

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))

问题

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.

相关错误

代码和消息文本来自 DuckDB documentation。浏览完整的 DuckDB error list 或者 搜索每个数据库错误.

// 错误更少,修复更快

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.

本页文字和数据: CC BY 4.0,请注明出处 SQLly (sqlly.app)。 代码和 SQL 片段: MIT. 许可详情 引用的错误消息文本归其数据库厂商所有。