Skip to content

Docs: PostgreSQL generated columns can be used in PK/UNIQUE/FK (non-partitioned tables) #608

@thenovatasha

Description

@thenovatasha

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions