Why it happens: INSERT has a different number of columns and values
CREATE TABLE products (
id INTEGER PRIMARY KEY,
name TEXT NOT NULL,
price NUMERIC DEFAULT 0
);
INSERT INTO products (id, name, price)
VALUES (1, 'Keyboard');The target list contains id, name and price, but VALUES supplies two expressions. There is no one-to-one match.
Check the result with small data
A CTE with a column list follows the same rule as INSERT: three column names, three values in each row.
WITH new(id, name, price) AS (VALUES (2, 'Mouse', 24.90))
SELECT id, name, price FROM new;| id | name | price |
|---|---|---|
| 2 | Mouse | 24.9 |
Each value takes the position of its column: 2 goes to id, Mouse to name and 24.90 to price. If you remove 24.90, SQLite rejects the query with "table new has 2 values for 3 columns", the same kind of error as in the INSERT. Do not fix it by removing a needed value at random.
Check your understanding: If you list three columns, how many values should you supply in that row?
Show answer
Three, one for each column and in the same order. If a column should take its DEFAULT, remove it from the list instead of leaving a gap.
How to fix it in SQL
Fix 1: leave out the column that should use DEFAULT
INSERT INTO products (id, name)
VALUES (1, 'Keyboard');| id | name | price |
|---|---|---|
| 1 | Keyboard | 0 |
Fix 2: supply every listed value
INSERT INTO products (id, name, price)
VALUES (2, 'Mouse', 24.90);Both forms are valid because every expression in VALUES has an unambiguous target column.
How to check it
Count the expressions in each row and check the values and defaults that end up in the table.
How to avoid it next time
Adding NULL to make the count match may break NOT NULL and does not apply DEFAULT.
Related cases worth checking
A INSERT without a column list depends more on the table's physical structure and defined column order. Writing INSERT INTO products (id, name, price) makes the intent visible and lets you leave out columns that should get a default or NULL where the schema allows it.
Diagnose in layers: first check the number and order of the columns; then review types, NOT NULL, UNIQUE, FOREIGN KEY and the other constraints.