Skip to content

AutoMigrate creating different unique Index on subsequent runs #6378

@cabeedelhertz

Description

@cabeedelhertz

GORM Playground Link

go-gorm/playground#608

Description

I have an existing database and consistently run Automigrate but after upgrading my gorm version from 1.28.3, the automigrate is creating different unique indexes than the ones I have defined and already exist in my database.

To reproduce, pull this branch and run GORM_DIALECT=postgres go test. This should work and pass tests. But if you run it again, it will fail to make migrations because it is trying to create a uniqueIndex and uniqueConstraint that doesn't have the same name or where clause as the unique index that I defined:
ERROR: could not create unique index "idx_users_description" (SQLSTATE 23505)

After the initial run of AutoMigrate the unique indexes that I defined are correctly created:
image

But after the next time I run AutoMigrate, it created new unique indexes with a different name and without the where clause:
image

I can only get to the state of the second picture if I don't have any data that conflicts the where clause of the unique index of the description column. In my use case, I have a table with a unique index with a where deleted_at is NULL condition but now that my AutoMigrate behaves differently, I cannot run migrations.

Did something change with how AutoMigrate handles unique indexes?

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions