Why it happens: NOT NULL rejects a missing value
CREATE TABLE products (
id INTEGER PRIMARY KEY,
name TEXT NOT NULL,
active INTEGER NOT NULL DEFAULT 1
);
INSERT INTO products (id, name)
VALUES (1, NULL);The column name is present in the INSERT, but its value is explicitly NULL. The database must reject it because it breaks the declared rule.
Check the result with small data
Before the INSERT, we can check which rows are missing a required field.
WITH input(name, price) AS (
VALUES (NULL, 25), ('Keyboard', 40)
)
SELECT price, CASE WHEN name IS NULL
THEN 'Reject' ELSE 'Valid' END AS validation
FROM input ORDER BY price;| price | validation |
|---|---|
| 25 | Reject |
| 40 | Valid |
The row with a price of 25 must be rejected because the name is missing. Do not replace it with an empty string without checking whether that is correct data.
Check your understanding: Would it be acceptable to leave name empty if the schema only says NOT NULL?
Show answer
The constraint would accept it, because an empty string is not NULL. But it is not a valid name: if it must not be allowed, add a rule such as CHECK (name <> '').
How to fix it in SQL
INSERT INTO products (id, name)
VALUES (1, 'Keyboard');Also note that active can be omitted because it has DEFAULT 1. That doesn't mean that DEFAULT turns an explicit NULL into 1: they are different ideas. If you supply NULL to a column NOT NULL, the operation is still invalid.
How to check it
Inspect the value reaching the column and run the corrected INSERT on a copy.
How to avoid it next time
DEFAULT does not, on its own, replace an explicit NULL.
Related cases worth checking
If the missing value is legitimate, the question changes: perhaps the constraint does not represent the domain correctly. Before altering the table, confirm whether "unknown" or "not provided yet" is a valid state for that column. If it is, document the model change; if not, fix the flow that produces the missing value.