db.Select("Name").Create(&user) // INSERT INTO "users" (name) VALUES ("jinzhu", 1, 2);
db.Omit("BillingAddress").Create(&user) // Skip create BillingAddress when creating a user
db.Omit(clause.Associations).Create(&user) // Skip all associations when creating a user
NOTE: For many2many associations, GORM will upsert the associations before creating the join table references, if you want to skip the upserting of associations, you could skip it like:
db.Omit("Languages.*").Create(&user)
The following code will skip the creation of the association and its references
// Create user and his BillingAddress, ShippingAddress // When creating the BillingAddress only use its address1, address2 fields and omit others db.Select("BillingAddress.Address1", "BillingAddress.Address2").Create(&user)
Association Mode contains some commonly used helper methods to handle relationships
// Start Association Mode var user User db.Model(&user).Association("Languages") // `user` is the source model, it must contains primary key // `Languages` is a relationship's field name // If the above two requirements matched, the AssociationMode should be started successfully, or it should return error db.Model(&user).Association("Languages").Error
Remove all reference between source & association, won’t delete those associations
db.Model(&user).Association("Languages").Clear()
Count Associations
Return the count of current associations
db.Model(&user).Association("Languages").Count()
// Count with conditions codes := []string{"zh-CN", "en-US", "ja-JP"} db.Model(&user).Where("code IN ?", codes).Association("Languages").Count()
Batch Data
Association Mode supports batch data, e.g:
// Find all roles for all users db.Model(&users).Association("Role").Find(&roles)
// Delete User A from all user's team db.Model(&users).Association("Team").Delete(&userA)
// Get distinct count of all users' teams db.Model(&users).Association("Team").Count()
// For `Append`, `Replace` with batch data, the length of the arguments needs to be equal to the data's length or else it will return an error var users = []User{user1, user2, user3} // e.g: we have 3 users, Append userA to user1's team, append userB to user2's team, append userA, userB and userC to user3's team db.Model(&users).Association("Team").Append(&userA, &userB, &[]User{userA, userB, userC}) // Reset user1's team to userA,reset user2's team to userB, reset user3's team to userA, userB and userC db.Model(&users).Association("Team").Replace(&userA, &userB, &[]User{userA, userB, userC})
Delete with Select
You are allowed to delete selected has one/has many/many2many relations with Select when deleting records, for example:
// delete user's account when deleting user db.Select("Account").Delete(&user)
// delete user's Orders, CreditCards relations when deleting user db.Select("Orders", "CreditCards").Delete(&user)
// delete user's has one/many/many2many relations when deleting user db.Select(clause.Associations).Delete(&user)
// delete each user's account when deleting users db.Select("Account").Delete(&users)
NOTE: Associations will only be deleted if the deleting records’s primary key is not zero, GORM will use those primary keys as conditions to delete selected associations
// DOESN'T WORK db.Select("Account").Where("name = ?", "jinzhu").Delete(&User{}) // will delete all user with name `jinzhu`, but those user's account won't be deleted
db.Select("Account").Where("name = ?", "jinzhu").Delete(&User{ID: 1}) // will delete the user with name = `jinzhu` and id = `1`, and user `1`'s account will be deleted
db.Select("Account").Delete(&User{ID: 1}) // will delete the user with id = `1`, and user `1`'s account will be deleted
Association Tags
Tag
Description
foreignKey
Specifies column name of the current model that is used as a foreign key to the join table
references
Specifies column name of the reference’s table that is mapped to the foreign key of the join table
polymorphic
Specifies polymorphic type such as model name
polymorphicValue
Specifies polymorphic value, default table name
many2many
Specifies join table name
joinForeignKey
Specifies foreign key column name of join table that maps to the current table
joinReferences
Specifies foreign key column name of join table that maps to the reference’s table