generated always
SQLSTATE 428C9 is raised when an INSERT or UPDATE explicitly provides a value for a column defined as GENERATED ALWAYS AS IDENTITY or GENERATED ALWAYS AS (expression). These columns are managed entirely by Postgres and cannot be set by the user.
- 1INSERT provides a value for a GENERATED ALWAYS AS IDENTITY column
- 2UPDATE sets a value on a GENERATED ALWAYS computed column
INSERT providing a value for a GENERATED ALWAYS identity column.
CREATE TABLE orders ( id BIGINT GENERATED ALWAYS AS IDENTITY PRIMARY KEY, total NUMERIC ); INSERT INTO orders (id, total) VALUES (999, 100.00); -- 428C9
expected output
ERROR: cannot insert a non-DEFAULT value into column "id" DETAIL: Column "id" is an identity column defined as GENERATED ALWAYS.
Fix 1
Omit the generated column from the INSERT column list
WHEN When inserting into a table with GENERATED ALWAYS AS IDENTITY.
INSERT INTO orders (total) VALUES (100.00); -- id generated automatically
Why this works
Omitting the identity column from the INSERT list lets Postgres generate the value automatically.
Fix 2
Use OVERRIDING SYSTEM VALUE if you must supply an identity value
WHEN During data migration where specific identity values must be preserved.
INSERT INTO orders (id, total) OVERRIDING SYSTEM VALUE VALUES (999, 100.00);
Why this works
OVERRIDING SYSTEM VALUE explicitly bypasses the GENERATED ALWAYS restriction, permitting a user-supplied value. Use only for data migration.
Fix 3
Use GENERATED BY DEFAULT AS IDENTITY instead for more flexibility
WHEN When the column should allow user-supplied values in some cases.
ALTER TABLE orders ALTER COLUMN id SET GENERATED BY DEFAULT;
Why this works
BY DEFAULT allows user-supplied values without requiring OVERRIDING SYSTEM VALUE.
GENERATED AS IDENTITY introduced in Postgres 10. GENERATED ALWAYS AS (expression) computed columns added in Postgres 12.
Class 42 — Syntax Error or Access Rule Violation
Content generated with AI assistance and reviewed for accuracy. Found an error? hello@errcodes.dev