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
AutoMigrate fails if custom index is already added to the table (Duplicate column error) #5282
Comments
This is due to SQLite driver does not expect index name in The misleading error |
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: With Versions: Have had issues with this for a number of weeks now and having to do some hacky stuff to get migrations working... |
I'm using the latest versions: gorm.io/datatypes v1.0.7 And am still running into the same issue:
|
I switched back to the following versions and the issue went away: gorm.io/datatypes v1.0.2 |
@khalilsarwari It is work for me in lastest version, please provide it in https://github.com/go-gorm/playground |
@a631807682 You are right, it is also working for me even when I try gorm.io/datatypes v1.0.7 I'm not too sure why it was giving me the same error, perhaps I was not updating the versions correctly. |
I can reproduce this on postgres, using versions:
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)
In this environment, when I start up my service, automigration fails with am error: 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:
I can then recreate the error by adding the following constraint in my dev environment ( |
I'm facing a similar issue today with GORM v1.23.8 and CockroachDB. Interestingly enough, if you don't set an index name ( For now the simplest workaround is to not make the index unique. |
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. |
Tested with latest version, should works with sqlite, mysql and postgres. |
This issue fixed for me when I changed unique index name like:
make sure to remove table name from the index name |
I didn't give my index any name.
it fails with: on a Postgres 14 database. Worked for me with |
GORM Playground Link
go-gorm/playground#469
Description
AutoMigrate fails if custom index is already added to the table (Duplicate column error)
The text was updated successfully, but these errors were encountered: