gorm icon indicating copy to clipboard operation
gorm copied to clipboard

AutoMigrate fails if custom index is already added to the table (Duplicate column error)

Open glebarez opened this issue 2 years ago • 9 comments

GORM Playground Link

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

Description

AutoMigrate fails if custom index is already added to the table (Duplicate column error)

glebarez avatar Apr 21 '22 13:04 glebarez

This is due to SQLite driver does not expect index name in CREATE INDEX to be written without any quotes. Fix PR opened: https://github.com/go-gorm/sqlite/pull/90

The misleading error duplicate column name: id was due to bug in GORM migrator (Fix filed in https://github.com/go-gorm/gorm/pull/5283)

glebarez avatar Apr 21 '22 14:04 glebarez

Not just a Sqlite issue, I just got this in postgres as well with the latest gorm and postgres driver versions

I'm getting errors that look like: relation "idx_signup_requests_hash_code" already exists (SQLSTATE 42P07)

With Versions: gorm.io/gorm v1.23.4 gorm.io/driver/postgres v1.3.4

Have had issues with this for a number of weeks now and having to do some hacky stuff to get migrations working...

LouisSayers avatar Apr 22 '22 03:04 LouisSayers

I'm using the latest versions:

gorm.io/datatypes v1.0.7 gorm.io/driver/postgres v1.3.8 gorm.io/gorm v1.23.7

And am still running into the same issue:

relation "idx_users_user_name" already exists (SQLSTATE 42P07)

khalilsarwari avatar Jul 05 '22 07:07 khalilsarwari

I switched back to the following versions and the issue went away:

gorm.io/datatypes v1.0.2 gorm.io/driver/postgres v1.1.1 gorm.io/gorm v1.21.15

khalilsarwari avatar Jul 05 '22 07:07 khalilsarwari

@khalilsarwari It is work for me in lastest version, please provide it in https://github.com/go-gorm/playground

a631807682 avatar Jul 05 '22 16:07 a631807682

@a631807682 You are right, it is also working for me even when I try

gorm.io/datatypes v1.0.7 gorm.io/driver/postgres v1.3.8 gorm.io/gorm v1.23.7

I'm not too sure why it was giving me the same error, perhaps I was not updating the versions correctly.

khalilsarwari avatar Jul 21 '22 20:07 khalilsarwari

I can reproduce this on postgres, using versions:

gorm.io/driver/postgres v1.3.8 (previously 1.1.0)
gorm.io/gorm v1.23.8 (previously 1.21.13)

TL;DR:

The trick is to create a table with both an index AND a constraint on the same column. This causes the automigration to fail.

Steps:

My production database originally didn't use gorm, so has some manually created constraints (backed by indexes, because postgres) Namely, the output of \d+ <tablename> shows the following indexes:

Indexes:
...
    "idx_users_user_id" UNIQUE, btree (user_id)
    "users_user_id_key" UNIQUE CONSTRAINT, btree (user_id)
...

In this environment, when I start up my service, automigration fails with am error: ERROR: relation \"idx_users_user_id\" already exists (SQLSTATE 42P07).

However, if I create a new database purely through gorm (in a dev environment), the result is missing the CONSTRAINT, and that starts up fine:

Indexes:
...
    "idx_users_user_id" UNIQUE, btree (user_id)
...

I can then recreate the error by adding the following constraint in my dev environment (alter table users add constraint "users_user_id_key" unique (user_id)). Upon restarting the service, automigration then fails with the error mentioned above.

PaulSonOfLars avatar Aug 07 '22 09:08 PaulSonOfLars

I'm facing a similar issue today with GORM v1.23.8 and CockroachDB.

image

image

Interestingly enough, if you don't set an index name (gorm:"uniqueIndex"), automigration fails on the second run as expected but if you do (gorm:"uniqueIndex:auth_token") it only ever fails on the third run.

For now the simplest workaround is to not make the index unique.

HeCorr avatar Sep 09 '22 15:09 HeCorr

Actually, I think my issue is unrelated and I managed to fix it by defining the unique tag separately:

image

HeCorr avatar Sep 09 '22 15:09 HeCorr

Are there any updates on this? Would be lovely get this fixed.

I'd be happy to give it a go, but I have no familiarity with the codebase and wouldn't know where to start looking. If others don't have time to debug, could they maybe point me in the right direction?

PaulSonOfLars avatar Dec 03 '22 16:12 PaulSonOfLars

Are there any updates on this? Would be lovely get this fixed.

I'd be happy to give it a go, but I have no familiarity with the codebase and wouldn't know where to start looking. If others don't have time to debug, could they maybe point me in the right direction?

This issue was originaly filed for Sqlite driver, and fixed afterwards. I suggest you file separate issue for whatever driver you have this failure with.

glebarez avatar Dec 04 '22 05:12 glebarez

Tested with latest version, should works with sqlite, mysql and postgres.

jinzhu avatar Jan 02 '23 13:01 jinzhu

This issue fixed for me when I changed unique index name like:

type User struct {
	Username     string `gorm:"uniqueIndex:idx_username"` // set index name without table name in the middle
}

make sure to remove table name from the index name auto name is like idx_users_username after remove is like idx_username

MMN3003 avatar Feb 06 '23 13:02 MMN3003

I didn't give my index any name.

Hash []byte `gorm:"size:49;uniqueIndex"`

it fails with: ERROR: relation "idx_transactions_hash" already exists (SQLSTATE 42P07)

on a Postgres 14 database.

Worked for me with v1.24.2 Didn't work with v1.24.5

shufps avatar Feb 18 '23 07:02 shufps