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

Unique field always causes field migration #5950

Closed
Nomango opened this issue Dec 30, 2022 · 5 comments
Closed

Unique field always causes field migration #5950

Nomango opened this issue Dec 30, 2022 · 5 comments
Assignees
Labels

Comments

@Nomango
Copy link
Contributor

Nomango commented Dec 30, 2022

GORM Playground Link

No time to make it for now.

Description

unique Field (having uniqueIndex or index:,unique tag) always causes migration.

The reason is this line:

Unique: utils.CheckTruth(tagSetting["UNIQUE"]),

field.Unique is only true when tagSettings["UNIQUE"] is true.

But uniqueIndex tag only sets tagSettings["UNIQUEINDEX"] and index tag only sets tagSettings["INDEX"].

And field migration is checking field.Unique in this line:

if unique, ok := columnType.Unique(); ok && unique != field.Unique {

Therefore field migration will always be performed.

Maybe schema/field.go#L116 should be changed to

field := &Field{
    ...,
    Unique: false,
}
if indexes, err := parseFieldIndexes(field); err == nil {
    for _, index := range indexes {
        if index.Class == "UNIQUE" {
            field.Unique = true
            break
        }
    }
}

But I don't think this treatment is good. Does field.Unique deserve to exist?

@github-actions github-actions bot added the type:missing reproduction steps missing reproduction steps label Dec 30, 2022
@Nomango Nomango changed the title Tag uniqueIndex causes field migration Tag uniqueIndex always causes field migration Dec 30, 2022
@github-actions
Copy link

The issue has been automatically marked as stale as it missing playground pull request link, which is important to help others understand your issue effectively and make sure the issue hasn't been fixed on latest master, checkout https://github.com/go-gorm/playground for details. it will be closed in 30 days if no further activity occurs. if you are asking question, please use the Question template, most likely your question already answered https://github.com/go-gorm/gorm/issues or described in the document https://gorm.ioSearch Before Asking

@Nomango Nomango changed the title Tag uniqueIndex always causes field migration Unique field always causes field migration Dec 30, 2022
@github-actions
Copy link

The issue has been automatically marked as stale as it missing playground pull request link, which is important to help others understand your issue effectively and make sure the issue hasn't been fixed on latest master, checkout https://github.com/go-gorm/playground for details. it will be closed in 30 days if no further activity occurs. if you are asking question, please use the Question template, most likely your question already answered https://github.com/go-gorm/gorm/issues or described in the document https://gorm.ioSearch Before Asking

@Nomango
Copy link
Contributor Author

Nomango commented Dec 30, 2022

Seems like #5942 is the same issue.

@github-actions
Copy link

The issue has been automatically marked as stale as it missing playground pull request link, which is important to help others understand your issue effectively and make sure the issue hasn't been fixed on latest master, checkout https://github.com/go-gorm/playground for details. it will be closed in 30 days if no further activity occurs. if you are asking question, please use the Question template, most likely your question already answered https://github.com/go-gorm/gorm/issues or described in the document https://gorm.ioSearch Before Asking

@jinzhu
Copy link
Member

jinzhu commented Jan 2, 2023

Should be fixed in commit 3d91802

@jinzhu jinzhu closed this as completed Jan 2, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants