Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Newer versions of GORM remove the unique index incorrectly #287

Open
Ponywka opened this issue Sep 19, 2024 · 3 comments
Open

Newer versions of GORM remove the unique index incorrectly #287

Ponywka opened this issue Sep 19, 2024 · 3 comments
Assignees

Comments

@Ponywka
Copy link

Ponywka commented Sep 19, 2024

GORM Playground Link

<I have no time for write tests for your "playground" while working at office, sorry. Maybe later>

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:

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 "<SERVICE>"."<TABLE>" DROP CONSTRAINT "uni_<TABLE>_<FIELD>"
ERR: models automigrating error: ERROR: constraint "uni_<TABLE>_<FIELD>" of relation "<TABLE>" does not exist (SQLSTATE 42704)

After researching the issue, it was discovered that I have an existing record:

ALTER TABLE ONLY "<SERVICE>"."<TABLE>" ADD CONSTRAINT idx_<TABLE>_<FIELD> UNIQUE (<FIELD>);

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

@LynxUA
Copy link

LynxUA commented Sep 25, 2024

It's the same issue for me. It is introduced in 1.5.7 and caused by the changes in go-gorm/gorm#6822

@alexei-g-aloteq
Copy link

alexei-g-aloteq commented Sep 26, 2024

It's the same issue for me. It is introduced in 1.5.7 and caused by the changes in go-gorm/gorm#6822

I had to fallback to:

gorm.io/driver/postgres v1.5.4
gorm.io/gorm v1.25.5

@LynxUA
Copy link

LynxUA commented Sep 27, 2024

It's the same issue for me. It is introduced in 1.5.7 and caused by the changes in go-gorm/gorm#6822

I had to fallback to:

gorm.io/driver/postgres v1.5.4
gorm.io/gorm v1.25.5

My bad. I wanted to specify gorm.io/gorm v1.25.7

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants