-
-
Notifications
You must be signed in to change notification settings - Fork 4k
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
refactor: distinguish between Unique and UniqueIndex #6386
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we add more tests showing what this change fixes? The test doesn't need to be successful as I can imagine this would involve a lot of driver changes.
I take mysql as an example
- We need to ensure that the field A marked as unique index is not
a varchar(20) unique
when creating table, butUNIQUE INDEX idx_a (a)
Link AutoMigrate creating different unique Index on subsequent runs #6378 - When comparing field A, we may need to compare
field.Unique
andfield.UniqueIndex
respectively (this may cause failure, because currently mysql does not handle the columnKey value'MUL'
, and unique index may have multiple)
Link 标签 uniqueIndex 在解析唯一性时未被判断,查看源代码发现仅判断了UNIQUE #5681
https://github.com/go-gorm/mysql/blob/master/migrator.go#L264 - When comparing, it is necessary to ensure the deletion and addition of the unique index
Link Unnecessarily unique indexes created when runningAutoMigrate
#6224
Since this PR may contain relatively large changes, I want to describe it here. This is what I can think of so far. If this PR will fix more problems, welcome to add.
Can I import |
Both ok for me. |
What should we do with
|
This situation is meaningless, it is equivalent to
|
Yes it's meaningless, but it parses out two IndexOption. Or in this case: type UserIndex struct {
Name8 string `gorm:"index:,length:10;index:,collate:utf8"`
} It got index "idx_user_indices_name8": {
Name: "idx_user_indices_name8",
Type: "",
Fields: []schema.IndexOption{
{Field: &schema.Field{Name: "Name8"}, Length: 10},
{Field: &schema.Field{Name: "Name8"}, Collate: "utf8"},
},
} Maybe Index.Fields ([]IndexOption) should be changed to Map[string]IndexOption which key is field name. If changed, do we mark Fields as Deprecated and add FieldMap, or just modify? |
The same index may indeed be created multiple times, but I am not sure whether the actual usage scenario exists. In this case, I can only think that some scenarios may need to be specified using FORCE INDEX, but in this case, I think users can specify an index name instead of using the default name (Using the default name may not be good for our comparison index). In short, I think there are too few usage scenarios to consider this situation, of course if my thinking is wrong, please let me know. |
I mean, it's an unreported bug. What is your opinion? fix or not? |
We don't have to fix it, specifying the index name can avoid. |
For CREATE TABLE `index_tests` (
`name` VARCHAR(10),
CONSTRAINT `uni_index_tests_name` UNIQUE (`name`)
); or CREATE TABLE `index_tests` (
`name` VARCHAR(10),
UNIQUE INDEX `idx_index_tests_name` (`name`)
); There is no difference between them in mysql, For option 1, I should add a |
Consistent with the current, we need to remove the redundant unique statement. type User struct {
ID uint `gorm:"primarykey"`
UI string `gorm:"type:varchar(20);uniqueIndex"`
U string `gorm:"type:varchar(20);unique"`
} CREATE TABLE `users` (
`id` BIGINT UNSIGNED AUTO_INCREMENT,
- `ui` VARCHAR ( 20 ) UNIQUE,
+ `ui` VARCHAR ( 20 ),
`u` VARCHAR ( 20 ) UNIQUE,
PRIMARY KEY ( `id` ),
INDEX `idx_users_deleted_at` ( `deleted_at` ),
UNIQUE INDEX `idx_users_ui` ( `ui` )) |
There is no doubt that we need to remove the redundant unique statement. + UNIQUE INDEX `idx_users_ui` ( `ui` ))
or
+ CONSTRAINT `idx_users_ui` UNIQUE (`ui`) |
UNIQUE INDEX `idx_users_ui` ( `ui` )) |
I think it should be a constraint. |
When the user uses the It's a https://stackoverflow.com/questions/23542794/postgres-unique-constraint-vs-index |
For
+1
So we use the GORM tag: unique
- unique constraint
+ unique index But as you shared said, they have some subtle differences, such as error code, foreign key. summary:
|
It should be |
black-06 proposes that if unique is defined as unique index, it will be indistinguishable (and compared) in database query, and the database query result unique and uniqueIndex tags are the same (can't resolve #5401), so unique needs to be changed into a constraint. Record this comment. |
Are there any plans for an upcoming release? |
# Conflicts: # tests/go.mod # tests/migrate_test.go
I found that ci of GORM and drivers are deadlocked, they will never succeed. So I will split this mr. |
# Conflicts: # tests/go.mod
I rolled back a small part of them, otherwise this mr would never have passed the ci. Guys, now that ci is successful, let's review and merge it |
Can you rebase 9ad102b - ab75e42 to one commit? It has been too long since I approved these changes. I just want to see what has been changed after that. |
ab75e42
to
7e1b4c5
Compare
@a631807682 I have compressed them, please take time to review~ |
# Conflicts: # tests/go.mod
* refactor: distinguish between Unique and UniqueIndex * take care of unique created by old version * update gorm to master latest (for go-gorm/gorm#6386) * merge
* refactor: distinguish between Unique and UniqueIndex * rewrite MigrateColumnUnique * update gorm to master latest (for go-gorm/gorm#6386)
* refactor: distinguish between Unique and UniqueIndex * update gorm to master latest for go-gorm/gorm#6386
What did this pull request do?
See #6381
Related issues:
#6224
#5401
#5715
#5681
#6378
#6407
User Case Description