[BUG] Wrong statement when using `generated always as`

Open
#212 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
3/5
Estimated time
1-2 days
Newbie friendliness
52/100
Issue type
Bug
Clarity
Mostly clear
Activity status
Stale
Tech stack
go, postgres
Domain
databases

Research direction

Start by reproducing the issue with pg-schema-diff apply using the generated column definition and the provided Postgres versions. Trace how the schema diff represents generated expressions and verify that the migration plan preserves GENERATED ALWAYS AS and applies successfully without producing a DEFAULT expression.

Written by the indexing model from the issue text.

Description

bug
Describe the bug

When I pg-schema-diff apply after adding this column:

search_vector tsvector generated always as (
  to_tsvector('simple', title || ' ' || coalesce(artist, ''))
) stored

I get the following error:

Error: generating plan: validating migration plan: running migration plan: executing migration statement: {ALTER TABLE "public"."tabs" ADD COLUMN "search_vector" tsvector DEFAULT to_tsvector('simple'::regconfig, ((title || ' '::text) || COALESCE(artist, ''::text))) 3s 3s []}: ERROR: cannot use column reference in DEFAULT expression (SQLSTATE 0A000) 
diff.Plan{
    Statements: {
        {
            DDL:         "ALTER TABLE \"public\".\"tabs\" ADD COLUMN \"search_vector\" tsvector DEFAULT to_tsvector('simple'::regconfig, ((title || ' '::text) || COALESCE(artist, ''::text)))",
            Timeout:     3000000000,
            LockTimeout: 3000000000,
            Hazards:     nil,
        },
    },
    CurrentSchemaHash: "22b8e78772d29e38",
}

It seems that pg-schema-diff is changing the ...generated always as ... to ...default...

Expected behavior

Use generated always as.

Context

pg-schema-diff version: Version=v0.9.0
Postgres version: 17
pg-schema-diff apply --dsn url --schema-dir db/

Dominant language
Go
Stars
884
Forks
82
PR merge metrics
No merged PRs in 30d

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from stripe/pg-schema-diff

All issues in stripe/pg-schema-diff

Similar issues

More Go issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.