-
-
Notifications
You must be signed in to change notification settings - Fork 4.1k
Fix: Composite primary key with auto-increment value returns 0 after insert #6127
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
Conversation
… in composite primary key.
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 you add more tests to describe it? just like doing in playground.
…INCREMENT"], add test to check autoincrement:false create_test.go: remove unused code: drop table CompositeKeyProduct
} | ||
|
||
newProd := &CompositeKeyProduct{} | ||
if err := DB.First(&newProd).Error; err != nil { |
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.
This is not rigorous here. When I run the test repeatedly, the inserted record product id is self-increasing, so it will fail.
I suggest to do DropTable
before AutoMigrate
.
if err := DB.Migrator().DropTable(&CompositeKeyProduct{}); err != nil {
t.Fatalf("failed to migrate, got error %v", err)
}
* fix: unit test * fix create test #6127 (comment) * style: rename to adaptorSerializerModel
Fix #4930 workaround for databases that support auto-increment in composite primary key.
What did this pull request do?
Fix issue #4930
User Case Description
In MySQL, you can have composite primary key with auto-increment
For example:
https://github.com/nash90/playground/blob/7a82d16b1b1b626df01cf43089b302de7c2a6bd9/models.go#L63-L70
Upon insert new record into
CompositeKeyProduct
with the following values:https://github.com/nash90/playground/blob/7a82d16b1b1b626df01cf43089b302de7c2a6bd9/main_test.go#L26-L30
expected
actual
This Pull Request is aiming to fix the above case