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

Fix columnType.Unique() returns true for non-unique index DDL. #154

Merged
merged 1 commit into from
Jun 9, 2023

Conversation

jortel
Copy link
Contributor

@jortel jortel commented Jun 7, 2023

  • Do only one thing
  • Non breaking API changes
  • Tested

What did this pull request do?

Fixes an issue where the migrator determines that a column is unique (true) when referenced in an index DDL without further determining whether the index is unique. As a result, a model with a non-unique index tag is always be deemed different than the DDL which always triggers a table migration. The fix is to determine the column uniqueness based on whether the referencing index DDL is CREATE UNIQUE INDEX ... and not CREATE INDEX.

Set the ColumnType.UniqueValue based on the column being referenced in a UNIQUE index (not just any index).

User Case Description

Common use case of to add a non unique index on a foreign key to support cascade delete. As a result, the auto-migrate detecting a difference and ALWAYS is migrating the table.

The use case: model defined as Person with 1-* relation to Parent (so the index cannot be unique).

type Person struct {
    ParentID uint `gorm:"index;not-null"`
}
CREATE INDEX idx_person ON person (parentid)

Results in: ColumnType.UniqueValue = true.

Signed-off-by: Jeff Ortel <jortel@redhat.com>
@jinzhu jinzhu merged commit 397ec6f into go-gorm:master Jun 9, 2023
4 checks passed
jortel added a commit to konveyor/tackle2-hub that referenced this pull request Jun 12, 2023
Includes: go-gorm/sqlite#154

Signed-off-by: Jeff Ortel <jortel@redhat.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants