go-gorm / go-gorm/postgres

Newer versions of GORM remove the unique index incorrectly

Open
#287 9 comments 9 reactions 1 assignee Claimed by @jinzhu View on GitHub
Dominant language
Go
Stars
297
Forks
155
Avg merge
27m
Merged PRs (30d)
2

Description

## GORM Playground Link

## Description

I use `gorm.io/driver/postgres v1.5.9`, `gorm.io/gorm v1.25.11` and `go 1.22.6` in our project.

Sample environment:
```go
type Smth struct {
ID string `gorm:"uniqueIndex"`
SmthDifferentID *int64 `gorm:"uniqueIndex"`
}

db, err := gorm.Open(postgres.Open(dsn), &gorm.Config{
NamingStrategy: schema.NamingStrategy{TablePrefix: schemaName + "."},
})
if err != nil {
return fmt.Errorf("database connecting error: %w", err)
}

err = db.Exec("CREATE SCHEMA IF NOT EXISTS " + schemaName).Error
if err != nil {
return fmt.Errorf("creating schema error: %w", err)
}

err = db.AutoMigrate(&Smth{})
if err != nil {
return fmt.Errorf("models automigrating error: %w", err)
}
```

When I run this code in already initialized before database in older versions of GORM I got this (place `SmthDifferentID` instead of `FIELD`):
```
[rows:0] ALTER TABLE ""."" DROP CONSTRAINT "uni__"
ERR: models automigrating error: ERROR: constraint "uni__" of relation "" does not exist (SQLSTATE 42704)
```
After researching the issue, it was discovered that I have an existing record:
```SQL
ALTER TABLE ONLY ""."" ADD CONSTRAINT idx__ UNIQUE ();
```

GORM tries delete the `uni_...` index instead of `idx_...`

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.