mirror of https://github.com/go-gorm/gorm.git
fix: throw err if association model miss primary key (#5187)
This commit is contained in:
parent
9a4d10be64
commit
3d7019a7c2
|
@ -187,8 +187,11 @@ func (association *Association) Delete(values ...interface{}) error {
|
|||
tx := association.DB.Model(reflect.New(rel.Schema.ModelType).Interface())
|
||||
|
||||
_, pvs := schema.GetIdentityFieldValuesMap(association.DB.Statement.Context, reflectValue, rel.Schema.PrimaryFields)
|
||||
pcolumn, pvalues := schema.ToQueryValues(rel.Schema.Table, rel.Schema.PrimaryFieldDBNames, pvs)
|
||||
conds = append(conds, clause.IN{Column: pcolumn, Values: pvalues})
|
||||
if pcolumn, pvalues := schema.ToQueryValues(rel.Schema.Table, rel.Schema.PrimaryFieldDBNames, pvs); len(pvalues) > 0 {
|
||||
conds = append(conds, clause.IN{Column: pcolumn, Values: pvalues})
|
||||
} else {
|
||||
return ErrPrimaryKeyRequired
|
||||
}
|
||||
|
||||
_, rvs := schema.GetIdentityFieldValuesMapFromValues(association.DB.Statement.Context, values, primaryFields)
|
||||
relColumn, relValues := schema.ToQueryValues(rel.Schema.Table, foreignKeys, rvs)
|
||||
|
@ -199,8 +202,11 @@ func (association *Association) Delete(values ...interface{}) error {
|
|||
tx := association.DB.Model(reflect.New(rel.FieldSchema.ModelType).Interface())
|
||||
|
||||
_, pvs := schema.GetIdentityFieldValuesMap(association.DB.Statement.Context, reflectValue, primaryFields)
|
||||
pcolumn, pvalues := schema.ToQueryValues(rel.FieldSchema.Table, foreignKeys, pvs)
|
||||
conds = append(conds, clause.IN{Column: pcolumn, Values: pvalues})
|
||||
if pcolumn, pvalues := schema.ToQueryValues(rel.FieldSchema.Table, foreignKeys, pvs); len(pvalues) > 0 {
|
||||
conds = append(conds, clause.IN{Column: pcolumn, Values: pvalues})
|
||||
} else {
|
||||
return ErrPrimaryKeyRequired
|
||||
}
|
||||
|
||||
_, rvs := schema.GetIdentityFieldValuesMapFromValues(association.DB.Statement.Context, values, rel.FieldSchema.PrimaryFields)
|
||||
relColumn, relValues := schema.ToQueryValues(rel.FieldSchema.Table, rel.FieldSchema.PrimaryFieldDBNames, rvs)
|
||||
|
@ -229,8 +235,11 @@ func (association *Association) Delete(values ...interface{}) error {
|
|||
}
|
||||
|
||||
_, pvs := schema.GetIdentityFieldValuesMap(association.DB.Statement.Context, reflectValue, primaryFields)
|
||||
pcolumn, pvalues := schema.ToQueryValues(rel.JoinTable.Table, joinPrimaryKeys, pvs)
|
||||
conds = append(conds, clause.IN{Column: pcolumn, Values: pvalues})
|
||||
if pcolumn, pvalues := schema.ToQueryValues(rel.JoinTable.Table, joinPrimaryKeys, pvs); len(pvalues) > 0 {
|
||||
conds = append(conds, clause.IN{Column: pcolumn, Values: pvalues})
|
||||
} else {
|
||||
return ErrPrimaryKeyRequired
|
||||
}
|
||||
|
||||
_, rvs := schema.GetIdentityFieldValuesMapFromValues(association.DB.Statement.Context, values, relPrimaryFields)
|
||||
relColumn, relValues := schema.ToQueryValues(rel.JoinTable.Table, joinRelPrimaryKeys, rvs)
|
||||
|
|
|
@ -261,3 +261,27 @@ func TestSaveHasManyCircularReference(t *testing.T) {
|
|||
children, parent.Children)
|
||||
}
|
||||
}
|
||||
|
||||
func TestAssociationError(t *testing.T) {
|
||||
DB = DB.Debug()
|
||||
user := *GetUser("TestAssociationError", Config{Pets: 2, Company: true, Account: true, Languages: 2})
|
||||
DB.Create(&user)
|
||||
|
||||
var user1 User
|
||||
DB.Preload("Company").Preload("Pets").Preload("Account").Preload("Languages").First(&user1)
|
||||
|
||||
var emptyUser User
|
||||
var err error
|
||||
// belongs to
|
||||
err = DB.Model(&emptyUser).Association("Company").Delete(&user1.Company)
|
||||
AssertEqual(t, err, gorm.ErrPrimaryKeyRequired)
|
||||
// has many
|
||||
err = DB.Model(&emptyUser).Association("Pets").Delete(&user1.Pets)
|
||||
AssertEqual(t, err, gorm.ErrPrimaryKeyRequired)
|
||||
// has one
|
||||
err = DB.Model(&emptyUser).Association("Account").Delete(&user1.Account)
|
||||
AssertEqual(t, err, gorm.ErrPrimaryKeyRequired)
|
||||
// many to many
|
||||
err = DB.Model(&emptyUser).Association("Languages").Delete(&user1.Languages)
|
||||
AssertEqual(t, err, gorm.ErrPrimaryKeyRequired)
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue