GORM Playground Link

https://github.com/go-gorm/playground/pull/433

Description

Model:

type Group struct {
    ID    string `gorm:"primaryKey"`
    GID   uint32 `gorm:"not null;autoIncrement"`
    ...
}

Migrator error:

ERROR: type "bigserial" does not exist (SQLSTATE 42704)

Previously this table was created by GORM with type bigint. I'm using latest versions of both GORM (v1.22.5) and potgres-driver (v1.2.3).

4792 seems related, but there's nothing to upgrade to.

Switching to gorm:"type:bigserial" or gorm:"type:bigint;unique;autoIncrement" does not help.

Comment From: github-actions[bot]

The issue has been automatically marked as stale as it missing playground pull request link, which is important to help others understand your issue effectively and make sure the issue hasn't been fixed on latest master, checkout https://github.com/go-gorm/playground for details. it will be closed in 2 days if no further activity occurs. if you are asking question, please use the Question template, most likely your question already answered https://github.com/go-gorm/gorm/issues or described in the document https://gorm.io ✨ Search Before Asking

Comment From: roboslone

@jinzhu please take a look at this.

Comment From: roboslone

This patch seems to fix the test, but I doubt that's the right way to do it.

func (m Migrator) AlterColumn(value interface{}, field string) error {
    return m.RunWithValue(value, func(stmt *gorm.Statement) error {
        if field := stmt.Schema.LookUpField(field); field != nil {
            fileType := clause.Expr{SQL: m.DataTypeOf(field)}
+
+           switch (fileType.SQL) {
+           case "smallserial":
+               fileType.SQL = "smallint"
+           case "serial":
+               fileType.SQL = "integer"
+           case "bigserial":
+               fileType.SQL = "bigint"
+           }
+
            return m.DB.Exec(
                "ALTER TABLE ? ALTER COLUMN ? TYPE ?",
                m.CurrentTable(stmt), clause.Column{Name: field.DBName}, fileType,
            ).Error

        }
        return fmt.Errorf("failed to look up field with name: %s", field)
    })
}

Comment From: roboslone

Maybe it's better to not alter such columns at all:

func (m Migrator) AlterColumn(value interface{}, field string) error {
    return m.RunWithValue(value, func(stmt *gorm.Statement) error {
        if field := stmt.Schema.LookUpField(field); field != nil {
            fileType := clause.Expr{SQL: m.DataTypeOf(field)}

+           switch fileType.SQL {
+           case "smallserial":
+               fallthrough
+           case "serial":
+               fallthrough
+           case "bigserial":
+               return nil
+           }

            return m.DB.Exec(
                "ALTER TABLE ? ALTER COLUMN ? TYPE ?",
                m.CurrentTable(stmt), clause.Column{Name: field.DBName}, fileType,
            ).Error

        }
        return fmt.Errorf("failed to look up field with name: %s", field)
    })
}

Comment From: roboslone

Any luck here? Can I do anything to help resolve this?

Comment From: Genora51

I'm experiencing similar issues: any update on this @jinzhu ?

Comment From: a631807682

https://www.postgresql.org/docs/current/datatype-numeric.html#DATATYPE-SERIAL

Seems we need to do special handling for serial in AlterColumn, it still works in CreateTable

Comment From: Goldziher

i am having a similar issue with this:

    Position              int            `gorm:"type:serial" json:"position"`  

It tries to automigrate this column time-and-time again, even when i dump the DB and start from scratch.

Comment From: roboslone

Should this be resolved in v1.23.5? I still get the type "bigserial" does not exist (SQLSTATE 42704) error. Is there a version I can check?

Comment From: jinzhu

upgrade postgres driver.

On Tue, Apr 26, 2022 at 8:28 PM roboslone @.***> wrote:

Should this be resolved in v1.23.5? I still get the type "bigserial" does not exist (SQLSTATE 42704) error. Is there a version I can check?

— Reply to this email directly, view it on GitHub https://github.com/go-gorm/gorm/issues/5047#issuecomment-1109735444, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAABVO2TE6PD5AAM3EBPFI3VG7OQFANCNFSM5NO2INSQ . You are receiving this because you were mentioned.Message ID: @.***>

-- Best regards


Jinzhu github.com/jinzhu

Comment From: roboslone

Thanks, it works!

Comment From: Toflex

Thanks, it works!

What version of Postgres driver worked for you?

Comment From: roboslone

@Toflex gorm.io/driver/postgres v1.3.5

Comment From: ridwankustanto

I still get error type "bigserial" does not exist (SQLSTATE 42704) after update to gorm.io/driver/postgres v1.3.5 on alter table.