postgres icon indicating copy to clipboard operation
postgres copied to clipboard

feat: support PostgreSQL generated columns via the `generated` tag

Open h2zi opened this issue 5 months ago • 1 comments

What this adds

First-class support for PostgreSQL 10+ generated columns through a new generated struct tag, keeping the value-generation strategy separate from the column type (a generation strategy is not a data type). Relates to go-gorm/gorm#7191.

type Product struct {
    ID       uint    `gorm:"primaryKey;generated:identity"`              // bigint GENERATED BY DEFAULT AS IDENTITY
    Price    float64 `gorm:"type:numeric"`
    Quantity int
    Total    float64 `gorm:"->;type:numeric;generated:price * quantity"` // numeric GENERATED ALWAYS AS (price * quantity) STORED
}

Identity columns (the modern replacement for serial)

  • generated:identity → <int> GENERATED BY DEFAULT AS IDENTITY. BY DEFAULT is the default mode so the application can still supply explicit values (seeding, data migration) — matching the choice made by Django, EF Core/Npgsql, etc.
  • generated:identity always → <int> GENERATED ALWAYS AS IDENTITY (the database always generates the value). Keywords are order-independent (always identity also works).

Computed columns

  • Any other value is taken verbatim as the expression of a STORED computed column: generated:price * quantity → GENERATED ALWAYS AS (price * quantity) STORED. Commas inside the expression are preserved, so generated:coalesce(a, b) works. Combine with -> to make the column read-only (PostgreSQL forbids writing computed columns).

PostgreSQL is asymmetric here and the tag respects it: identity columns offer { ALWAYS | BY DEFAULT }, while computed columns are always GENERATED ALWAYS AS (...) STORED (there is no BY DEFAULT form).

Why a tag instead of type:

Putting bigint generated by default as identity inside type: conflates the data type with the generation strategy and causes real bugs: the clause leaks onto every referencing foreign key (go-gorm/gorm#7191, go-gorm/gorm#5222), and an auto-increment field's explicit identity type is silently overridden by the serial logic. Every mature ORM keeps generation separate from type (SQLAlchemy Identity/Computed, EF Core UseIdentityByDefaultColumn, Drizzle generatedByDefaultAsIdentity/generatedAlwaysAs, TypeORM @Generated("identity")). This tag follows that consensus.

Implementation

DataTypeOf parses the generated tag and renders the appropriate GENERATED ... clause; the base column type is computed exactly as before. No change to value binding — identity primary keys are already AutoIncrement (omitted from INSERT, read back via RETURNING), and computed columns rely on the existing -> read-only permission.

Tests & verification

  • Unit tests added to Test_DataTypeOf for both identity modes, order-independent keywords, computed columns, and comma-safe expressions.
  • Verified end-to-end against a real PostgreSQL 16 instance: CREATE TABLE emits the expected DDL for BY DEFAULT/ALWAYS identity and STORED computed columns; INSERT omits identity & computed columns and reads the identity value back via RETURNING; the computed value is correct; and repeated AutoMigrate runs are idempotent (no ALTER).
CREATE TABLE "products" ("id" bigint GENERATED BY DEFAULT AS IDENTITY,"name" text,"price" numeric,
  "quantity" bigint,"total" numeric GENERATED ALWAYS AS (price * quantity) STORED,PRIMARY KEY ("id"))
CREATE TABLE "events" ("id" bigint GENERATED ALWAYS AS IDENTITY,"name" text,PRIMARY KEY ("id"))

Related

The schema-side half of go-gorm/gorm#7191 — stopping foreign keys from inheriting a referenced key's value-generating clause — is fixed separately in go-gorm/gorm#7816.

🤖 Generated with Claude Code

h2zi avatar Jun 25 '26 10:06 h2zi

Heads-up: I've reworked this PR after the initial review. Instead of teaching getSchemaCustomType to preserve an identity clause written inside type: (which blesses an approach that conflates the column type with its generation strategy), this now adds a first-class generated tag for both identity and computed columns, and the base type rendering is left untouched. See the updated description for the full design rationale and the real-PostgreSQL verification.

h2zi avatar Jun 25 '26 11:06 h2zi