// DuckDB error reference

Constraint Error: A NOT NULL column received NULL

An INSERT or UPDATE tried to store NULL in a column declared NOT NULL, and DuckDB rejected the row.

Exception
Constraint Error
카테고리
Constraint Error
오류 메시지
Constraint Error: NOT NULL constraint failed: t.a

의미

DuckDB enforces constraints per row at write time. The message names the table and column, NOT NULL constraint failed: t.a, and the whole statement fails, so in a multi-row INSERT no row from it lands.

The NULL does not have to be literal. It can come from a LEFT JOIN that found no match, a DEFAULT that is itself NULL, or a SELECT whose WHERE clause filtered differently than the author expected. In COPY and INSERT from a query, one bad source row aborts the statement.

일반적인 원인

  • An INSERT that omits the column and the table has no non-NULL default for it.
  • A literal NULL or an expression that evaluates to NULL, often from a LEFT JOIN or a TRY_CAST that failed. A plain CAST that fails raises a Conversion Error instead.
  • An UPDATE that sets the column to NULL.
  • A COPY or INSERT INTO ... SELECT where a source row holds NULL in that position.

Find the cause

-- Which target columns are NOT NULL and what defaults do they have?
PRAGMA table_info('t');
-- Or with the catalog views:
SELECT column_name, is_nullable, column_default FROM information_schema.columns WHERE table_name = 't';
-- Which source rows would fail:
SELECT count(*) FROM stage WHERE a IS NULL;

해결 방법

  1. Find the rows that would violate the constraint before inserting:

    SELECT * FROM stage WHERE a IS NULL;
  2. Give the column a sensible fallback with COALESCE, or filter the rows out:

    INSERT INTO t (a) SELECT coalesce(a, 0) FROM stage;
    INSERT INTO t (a) SELECT a FROM stage WHERE a IS NOT NULL;
  3. Declare a DEFAULT so omitted columns never become NULL:

    ALTER TABLE t ALTER COLUMN a SET DEFAULT 0;

Other ways to handle it

If NULL is a legitimate state for the data, drop the NOT NULL constraint with ALTER TABLE ... ALTER COLUMN ... DROP NOT NULL instead of writing sentinel values such as empty strings or zero, which pollute every later query.

안전하게 재현하세요

CREATE TABLE t(a INT NOT NULL);
INSERT INTO t VALUES (NULL);   -- Constraint Error: NOT NULL constraint failed: t.a

질문

Does the whole INSERT fail or just the bad row?

The whole statement fails and no row from it is inserted. Use an INSERT ... SELECT with a WHERE that excludes the bad rows, or fix the source first.

관련 오류

코드와 메시지 텍스트의 출처는 다음과 같습니다 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. 라이선스 세부 정보 인용된 오류 메시지 텍스트의 권리는 해당 데이터베이스 공급업체에 있습니다.