Skip to content

[BUG] column already has a default value #182

@aleclarson

Description

@aleclarson

Describe the bug

Migrating a column from bigserial PRIMARY KEY to bigint PRIMARY KEY GENERATED ALWAYS AS IDENTITY results in the following error:

Error: generating plan: validating migration plan: running migration plan: executing migration statement: {ALTER TABLE "public"."test" ALTER COLUMN "id" ADD GENERATED ALWAYS AS IDENTITY (INCREMENT BY 1 MINVALUE 1 MAXVALUE 9223372036854775807 START WITH 1 CACHE 1 NO CYCLE) 3s 3s []}: ERROR: column "id" of relation "test" already has a default value (SQLSTATE 55000) 
diff.Plan{
    Statements: {
        {
            DDL:         "ALTER TABLE \"public\".\"test\" ALTER COLUMN \"id\" ADD GENERATED ALWAYS AS IDENTITY (INCREMENT BY 1 MINVALUE 1 MAXVALUE 9223372036854775807 START WITH 1 CACHE 1 NO CYCLE)",
            Timeout:     3000000000,
            LockTimeout: 3000000000,
            Hazards:     nil,
        },
        {
            DDL:         "ALTER TABLE \"public\".\"test\" ALTER COLUMN \"id\" DROP DEFAULT",
            Timeout:     3000000000,
            LockTimeout: 3000000000,
            Hazards:     nil,
        },
        {
            DDL:         "DROP SEQUENCE \"public\".\"test_id_seq\"",
            Timeout:     3000000000,
            LockTimeout: 3000000000,
            Hazards:     {
                {Type:"DELETES_DATA", Message:"By deleting a sequence, its value will be permanently lost"},
            },
        },
    },
    CurrentSchemaHash: "75630832189e22c6",
}

Expected behavior

Run the DROP DEFAULT command before the ADD GENERATED command.

To Reproduce

Before

CREATE TABLE test (
  id bigserial PRIMARY KEY
);

After

CREATE TABLE test (
  id bigint PRIMARY KEY GENERATED ALWAYS AS IDENTITY
);

Run pg-schema-diff apply after saving each time.

Context
pg-schema-diff version: 5fe8259
pg-schema-diff usage: CLI
Postgres version: 14

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions