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

Unnecessarily unique indexes created when running AutoMigrate #6224

Closed
shanehou opened this issue Apr 8, 2023 · 5 comments · May be fixed by go-gorm/mysql#120
Closed

Unnecessarily unique indexes created when running AutoMigrate #6224

shanehou opened this issue Apr 8, 2023 · 5 comments · May be fixed by go-gorm/mysql#120
Assignees

Comments

@shanehou
Copy link

shanehou commented Apr 8, 2023

GORM Playground Link

go-gorm/playground#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.

@a631807682
Copy link
Member

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).

@shanehou
Copy link
Author

shanehou commented May 14, 2023

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).

@a631807682
Copy link
Member

a631807682 commented May 15, 2023

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

@dpanic
Copy link

dpanic commented Aug 1, 2023

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

@a631807682
Copy link
Member

#6381 (comment)

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

Successfully merging a pull request may close this issue.

4 participants