E0013 generated-column-write
INSERT or UPDATE gives a value to a generated column.
| Code | Name | Category | Default |
|---|---|---|---|
E0013 | generated-column-write | correctness | error |
What it reports
An INSERT gives a value other than DEFAULT to a generated column (GENERATED ALWAYS AS (expr) STORED, MySQL / SQLite AS (expr)), or UPDATE sets one. In PostgreSQL the same holds for identity columns declared GENERATED ALWAYS AS IDENTITY. The database computes these values itself and rejects the statement.
The examples on this page also use this table:
CREATE TABLE line_items (
id INT GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
quantity INT NOT NULL,
unit_price NUMERIC(10, 2) NOT NULL,
amount NUMERIC(12, 2) GENERATED ALWAYS AS (quantity * unit_price) STORED
);
Example
INSERT INTO line_items (quantity, unit_price, amount) VALUES (2, 9.50, 19.00);
error[E0013]: Column 'amount' is a generated column: INSERT can't give it a value
--> q.sql:1:47
|
1 | INSERT INTO line_items (quantity, unit_price, amount) VALUES (2, 9.50, 19.00);
| ^^^^^^
= help: Leave the column out of the INSERT, or give it DEFAULT
Fixed:
INSERT INTO line_items (quantity, unit_price) VALUES (2, 9.50);
Notes
DEFAULT is always allowed, and so are GENERATED BY DEFAULT AS IDENTITY columns. Generated columns may be left out of an INSERT even when they are NOT NULL (E0008 doesn't report them). SQLite doesn't count generated columns in an INSERT without a column list.
Configuring
# sqlsift.toml
[rules]
generated-column-write = "warn" # or "off"
Or for a single line: -- sqlsift:disable E0013. The examples use the schema on the Rules page.