Golang Gorm 一对多 关联模式 Association + Append 添加关联 Association + Replace

添加关联


为 many to manyhas many 添加新的关联;为 has onebelongs to 替换当前的关联

db.Model(&user).Association("Languages").Append([]Language{languageZH, languageEN})

db.Model(&user).Association("Languages").Append(&Language{Name: "DE"})

db.Model(&user).Association("CreditCard").Append(&CreditCard{Number: "411111111111"})

 

 

替换关联


用一个新的关联替换当前的关联

db.Model(&user).Association("Languages").Replace([]Language{languageZH, languageEN})

db.Model(&user).Association("Languages").Replace(Language{Name: "DE"}, languageEN)

你可能感兴趣的:(Golang,Gorm,golang)