Automigrate creating different uniqueIndexes on subsequent runs #608
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Explain your user case and expected results
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 samename
orwhere
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:But after the next time I run

AutoMigrate
, it created new unique indexes with a different name and without the where clause: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 thedescription
column. In my use case, I have a table with a unique index with awhere deleted_at is NULL
condition but now that myAutoMigrate
behaves differently, I cannot run migrations.Did something change with how
AutoMigrate
handles unique indexes?