Hi! I found some problem with indexes in PostgreSQL. For example, you have some table

type USER struct {
    gorm.Model
    Name       string `json:"name" gorm:"type:varchar(64);not null"`
    Country   string `json:"country" gorm:"index:idx_country"`
}

and you want to change name for Country index, like a idx_country -> idx_country_name.

...
    Country   string `json:"country" gorm:"index:idx_country_name"`
...

After AutoMigration you see DB has two indexes idx_country and idx_country_name. Sometimes its broke app logic: if you remove uniq from code it's doesn't remove uniq from DB.

Affected versions gorm.io/driver/postgres v1.4.6 gorm.io/gorm v1.24.3

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 30 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: OscarMoya

GORM Playground Link

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

Description

Hi, I found a similar issue with Automigrate and Posgres that may be related to this one. The problem I found was not related with renaming, but with removing the index. I tried to submit a PR in gorm-playground here: https://github.com/go-gorm/playground/pull/562

Hope it depicts the problem I face correctly and helps to solve this issue.

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 30 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: 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 30 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: OscarMoya

@ex0hunt Would it be possible to add the PR I added (provided is OK with you) to the description so the Issue is marked as withReproduceSteps?

Thank you

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 30 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: a631807682

It WON’T delete unused columns to protect your data.

index is the same, you can remove it yourself using

    // Indexes
    CreateIndex(dst interface{}, name string) error
    DropIndex(dst interface{}, name string) error
    HasIndex(dst interface{}, name string) bool
    RenameIndex(dst interface{}, oldName, newName string) error
    GetIndexes(dst interface{}) ([]Index, error)

https://gorm.io/docs/migration.html#Auto-Migration

Comment From: OscarMoya

It WON’T delete unused columns to protect your data.

index is the same, you can remove it yourself using

// Indexes CreateIndex(dst interface{}, name string) error DropIndex(dst interface{}, name string) error HasIndex(dst interface{}, name string) bool RenameIndex(dst interface{}, oldName, newName string) error GetIndexes(dst interface{}) ([]Index, error)

https://gorm.io/docs/migration.html#Auto-Migration

The bug here reported, is that AutoMigrate does not drop the indexes automatically. if removed, should this supported? or is to be done Manually?