GORM Playground Link

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

Description

Model User has only one field Name, with uniqueIndex tag. First time I run AutoMigrate, GORM will create two unique indexes called idx_users_name and name. Then every time I run AutoMigrate, GORM will create a new unique index called name_2, name_3 and so on.

If I use uniqueIndex:i_u_name to specify the name of the unique index, the first run would still create i_u_name and name indexes, and the following runs would still create name_2, name_3 indexes.

Comment From: a631807682

This error is caused by the lack of distinction between Unique and UniqueIndex in schema.Filed and gorm.ColumnType. A good way is to record UniqueIndex in them and compare them, and a not so good way is to re-compare them in AlterColumn (like https://github.com/go-gorm/postgres/pull/157).

Comment From: shanehou

When AlterColumn, it'll run ALTER TABLE {table_name} MODIFY COLUMN {field} {fullDataType}. If fullDataType has UNIQUE, MySQL will always creates a new unique index.

So the solution is either checking if the unique index is already created before running the ALTER TABLE statement (your "not-so-good-way" I suppose?), or removing UNIQUE in fullDataType early (your "good-way" I suppose?). But I think the former way seems more reasonable, because fullDataType will clearly be used in other places like CreateTable.

This error is caused by the lack of distinction between Unique and UniqueIndex in schema.Filed and gorm.ColumnType. A good way is to record UniqueIndex in them and compare them, and a not so good way is to re-compare them in AlterColumn (like go-gorm/postgres#157).

Comment From: a631807682

When AlterColumn, it'll run ALTER TABLE {table_name} MODIFY COLUMN {field} {fullDataType}. If fullDataType has UNIQUE, MySQL will always creates a new unique index.

So the solution is either checking if the unique index is already created before running the ALTER TABLE statement (your "not-so-good-way" I suppose?), or removing UNIQUE in fullDataType early (your "good-way" I suppose?). But I think the former way seems more reasonable, because fullDataType will clearly be used in other places like CreateTable.

This error is caused by the lack of distinction between Unique and UniqueIndex in schema.Filed and gorm.ColumnType. A good way is to record UniqueIndex in them and compare them, and a not so good way is to re-compare them in AlterColumn (like go-gorm/postgres#157).

Because the migration needs to compare unique and unique index, they are also part of the migration. It is complicated for each driver to achieve such a comparison, and it is not conducive to expansion and maintenance, so the best way is to complete this repeated comparison by gorm operation, unless it cannot be done. Of course, we also welcome submitting PRs for comparison in the driver, as it is good for quick code fixes

Comment From: dpanic

I have the same problem. Any update on this? If you need any help, I can do some coding.

Comment From: a631807682

https://github.com/go-gorm/gorm/issues/6381#issuecomment-1929427042