# 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.

Source: https://sqlly.app/errors/duckdb/constraint-not-null
Last updated: 2026-11-30
License: Text and data [CC BY 4.0](https://creativecommons.org/licenses/by/4.0/) (credit SQLly, sqlly.app); code and SQL snippets [MIT](https://opensource.org/license/mit). [Details](https://sqlly.app/license).
Note: Quoted error message text belongs to its database vendor.

- Exception: `Constraint Error`
- Category: Constraint Error

Error message:

```text
Constraint Error: NOT NULL constraint failed: t.a
```

## What it means

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.

## Common causes

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

## How to fix it

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

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

   ```sql
   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:

   ```sql
   ALTER TABLE t ALTER COLUMN a SET DEFAULT 0;
   ```

## Reproduce it safely

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

## Questions

### 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.

## Related errors

- [`Constraint Error`](https://sqlly.app/errors/duckdb/constraint-unique): A duplicate key violates a unique constraint
- [`Constraint Error`](https://sqlly.app/errors/duckdb/constraint-check): A CHECK constraint rejected the row
- [`Conversion Error`](https://sqlly.app/errors/duckdb/conversion-could-not-convert): The value cannot be cast to the target type
- [`23502`](https://sqlly.app/errors/postgresql/23502): PostgreSQL: null value violates not-null constraint
- [`SQLITE_CONSTRAINT`](https://sqlly.app/errors/sqlite/sqlite-constraint): SQLite: constraint failed

The code and message text are from the [DuckDB documentation](https://duckdb.org/docs/).
