Doc Location: https://orm.drizzle.team/docs/generated-columns (PostgreSQL)
Problem:
Under limitations section, the page incorrectly says: “Cannot directly use in primary keys, foreign keys or unique constraints.”
In PostgreSQL, stored generated columns can be indexed and can serve as PRIMARY KEY (with NOT NULL) or **UNIQUE`, and can be referenced by foreign keys. The known limit is that a generated column cannot be a partition key.
Minimum proof of SQL:
CREATE TABLE t (
x int NOT NULL,
y int GENERATED ALWAYS AS (x * 2) STORED NOT NULL,
PRIMARY KEY (y) -- OK
);
CREATE TABLE child (
y_ref int REFERENCES t(y) -- OK: FK references generated PK
);
Also note that "VIRTUAL" is now available in PostgreSQL 18.