DB: Postgresql

Description

Looks like gorm 1.24.5 has an issue if you create a model with uniqueIndex attribute. Looks like previous versions of gorm did a "create unique index xxx" previously and now are using table constraints (e.g. "alter table add constraint ..." and these 2 things butt heads. Deleting the index and letting gorm recreate works as a workaround but isn't ideal.

Model example

type Teacher struct {
    ID      string   `gorm:"primaryKey;type:varchar(25)"`
    Subject string   `gorm:"uniqueIndex"`
}

Using auto migrate on the above model with a version of Gorm less than 1.24.3 will throw an error after upgrading to v1.24.5

relation \"idx_teacher_subject\" already exists (SQLSTATE 42P07)

Comment From: axiangcoding

I already created a related issue here: https://github.com/go-gorm/gorm/issues/6026

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

duplicated of https://github.com/go-gorm/gorm/issues/6026

Comment From: athoscouto

https://github.com/go-gorm/gorm/issues/6026 refers specifically to postgres. The fix looks like it is specific to postgres. Is it?

I'm also facing this problem with sqlite.

Comment From: axiangcoding

6026 refers specifically to postgres. The fix looks like it is specific to postgres. Is it?

I'm also facing this problem with sqlite.

It is specific to postgres. I can't reproduce sqlite error in https://github.com/go-gorm/playground/pull/561, maybe you can provide a PR to https://github.com/go-gorm/playground?

Comment From: golfapipol

same issue on postgresql with version 1.24.6

PhoneNumber         string                       `json:"phone_number" validate:"required" gorm:"uniqueIndex"`
ERROR: relation "idx_driver_accounts_phone_number" already exists (SQLSTATE 42P07)

Comment From: axiangcoding

same issue on postgresql with version 1.24.6

PhoneNumber string `json:"phone_number" validate:"required" gorm:"uniqueIndex"`

ERROR: relation "idx_driver_accounts_phone_number" already exists (SQLSTATE 42P07)

upgrade postgresql to 1.24.7 or higher will solve this problem