go-gorm / postgres

GORM PostgreSQL driver
MIT License
234 stars 123 forks source link

AutoMigration of XXXserial columns fails #161

Closed manstis closed 1 year ago

manstis commented 1 year ago

GORM Playground Link

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

Description

If you repeat the migration of a table with a column of type XXXserial the migration attempts to update the column and fails.

2023/02/21 15:00:00 /.../gorm-postgres/migrator.go:633 ERROR: type "bigserial" does not exist (SQLSTATE 42704)
[0.345ms] [rows:0] ALTER TABLE "table_structs" ALTER COLUMN "version" TYPE bigserial
    migrate_test.go:1512: 
            Error Trace:    /home/manstis/workspaces/git/manstis/forks/gorm-playground/gorm/tests/migrate_test.go:1512
            Error:          Auto migrate failed
            Test:           TestMigrationPostgresXXXSerial
--- FAIL: TestMigrationPostgresXXXSerial (0.10s)

You'll need to start Postgres

manstis commented 1 year ago

If you want to explain where the problem is likely to be I am happy to try a fix.

gorm\schema\field.go#ParseField has AutoIncrement set to false for XXXserial fields.

IDK if that is wrong; but I suspect since it is in the core gorm (and not a driver) that behaviour is correct.

gorm\migrator\migrator.go#MigrateColumn is where gorm sees the column as changed and tries to alter it.

manstis commented 1 year ago

Closing due to age.