forked from mirror/gorm
Fix create associations with zero primary key, close #4890
This commit is contained in:
parent
3a3b82263a
commit
8627634959
|
@ -207,7 +207,7 @@ func SaveAfterAssociations(create bool) func(db *gorm.DB) {
|
|||
}
|
||||
|
||||
cacheKey := utils.ToStringKey(relPrimaryValues)
|
||||
if len(relPrimaryValues) == 0 || (len(relPrimaryValues) == len(rel.FieldSchema.PrimaryFields) && !identityMap[cacheKey]) {
|
||||
if len(relPrimaryValues) != len(rel.FieldSchema.PrimaryFields) || !identityMap[cacheKey] {
|
||||
identityMap[cacheKey] = true
|
||||
if isPtr {
|
||||
elems = reflect.Append(elems, elem)
|
||||
|
|
|
@ -179,12 +179,8 @@ func TestForeignKeyConstraintsBelongsTo(t *testing.T) {
|
|||
|
||||
func TestFullSaveAssociations(t *testing.T) {
|
||||
coupon := &Coupon{
|
||||
ID: "full-save-association-coupon1",
|
||||
AppliesToProduct: []*CouponProduct{
|
||||
{
|
||||
CouponId: "full-save-association-coupon1",
|
||||
ProductId: "full-save-association-product1",
|
||||
},
|
||||
{ProductId: "full-save-association-product1"},
|
||||
},
|
||||
AmountOff: 10,
|
||||
PercentOff: 0.0,
|
||||
|
@ -198,11 +194,11 @@ func TestFullSaveAssociations(t *testing.T) {
|
|||
t.Errorf("Failed, got error: %v", err)
|
||||
}
|
||||
|
||||
if DB.First(&Coupon{}, "id = ?", "full-save-association-coupon1").Error != nil {
|
||||
if DB.First(&Coupon{}, "id = ?", coupon.ID).Error != nil {
|
||||
t.Errorf("Failed to query saved coupon")
|
||||
}
|
||||
|
||||
if DB.First(&CouponProduct{}, "coupon_id = ? AND product_id = ?", "full-save-association-coupon1", "full-save-association-product1").Error != nil {
|
||||
if DB.First(&CouponProduct{}, "coupon_id = ? AND product_id = ?", coupon.ID, "full-save-association-product1").Error != nil {
|
||||
t.Errorf("Failed to query saved association")
|
||||
}
|
||||
|
||||
|
@ -210,4 +206,18 @@ func TestFullSaveAssociations(t *testing.T) {
|
|||
if err := DB.Create(&orders).Error; err != nil {
|
||||
t.Errorf("failed to create orders, got %v", err)
|
||||
}
|
||||
|
||||
coupon2 := Coupon{
|
||||
AppliesToProduct: []*CouponProduct{{Desc: "coupon-description"}},
|
||||
}
|
||||
|
||||
DB.Session(&gorm.Session{FullSaveAssociations: true}).Create(&coupon2)
|
||||
var result Coupon
|
||||
if err := DB.Preload("AppliesToProduct").First(&result, "id = ?", coupon2.ID).Error; err != nil {
|
||||
t.Errorf("Failed to create coupon w/o name, got error: %v", err)
|
||||
}
|
||||
|
||||
if len(result.AppliesToProduct) != 1 {
|
||||
t.Errorf("Failed to preload AppliesToProduct")
|
||||
}
|
||||
}
|
||||
|
|
|
@ -62,15 +62,16 @@ type Language struct {
|
|||
}
|
||||
|
||||
type Coupon struct {
|
||||
ID string `gorm:"primarykey; size:255"`
|
||||
ID int `gorm:"primarykey; size:255"`
|
||||
AppliesToProduct []*CouponProduct `gorm:"foreignKey:CouponId;constraint:OnDelete:CASCADE"`
|
||||
AmountOff uint32 `gorm:"amount_off"`
|
||||
PercentOff float32 `gorm:"percent_off"`
|
||||
}
|
||||
|
||||
type CouponProduct struct {
|
||||
CouponId string `gorm:"primarykey; size:255"`
|
||||
ProductId string `gorm:"primarykey; size:255"`
|
||||
CouponId int `gorm:"primarykey;size:255"`
|
||||
ProductId string `gorm:"primarykey;size:255"`
|
||||
Desc string
|
||||
}
|
||||
|
||||
type Order struct {
|
||||
|
|
Loading…
Reference in New Issue