为 many to many
、has many
添加新的关联;为 has one
, belongs 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)
-
- var u User
- db.Take(&u, 1)
- var l Language
- db.Take(&l, 3)
- db.Model(&u).Association("Languages").Replace(&l)
如果存在,则删除源模型与参数之间的关系,只会删除引用,不会从数据库中删除这些对象。
- db.Model(&user).Association("Languages").Delete([]Language{languageZH, languageEN})
- db.Model(&user).Association("Languages").Delete(languageZH, languageEN)