Add test for composite key with auto-increment.
This commit is contained in:
parent
a6e90777e3
commit
414f34b00d
@ -318,5 +318,4 @@ func TestCompositePrimaryKeyWithAutoIncrement(t *testing.T) {
|
||||
f.Updatable = true
|
||||
f.Readable = true
|
||||
})
|
||||
|
||||
}
|
||||
|
@ -547,3 +547,33 @@ func TestFirstOrCreateRowsAffected(t *testing.T) {
|
||||
t.Fatalf("first or create rows affect err:%v rows:%d", res.Error, res.RowsAffected)
|
||||
}
|
||||
}
|
||||
|
||||
func TestCreateWithAutoIncrementCompositeKey(t *testing.T) {
|
||||
type CompositeKeyProduct struct {
|
||||
ProductID int `gorm:"primaryKey;autoIncrement:true;"` // primary key
|
||||
LanguageCode int `gorm:"primaryKey;"` // primary key
|
||||
Code string
|
||||
Name string
|
||||
}
|
||||
|
||||
err := DB.Migrator().DropTable(&CompositeKeyProduct{})
|
||||
if err = DB.AutoMigrate(&CompositeKeyProduct{}); err != nil {
|
||||
t.Fatalf("failed to migrate, got error %v", err)
|
||||
}
|
||||
|
||||
prod := &CompositeKeyProduct{
|
||||
LanguageCode: 56,
|
||||
Code: "Code56",
|
||||
Name: "ProductName56",
|
||||
}
|
||||
if err := DB.Create(&prod).Error; err != nil {
|
||||
t.Fatalf("failed to create, got error %v", err)
|
||||
}
|
||||
|
||||
newProd := &CompositeKeyProduct{}
|
||||
if err := DB.First(&newProd).Error; err != nil {
|
||||
t.Fatalf("errors happened when query: %v", err)
|
||||
} else {
|
||||
AssertObjEqual(t, newProd, prod, "ProductID", "LanguageCode", "Code", "Name")
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user