Auto Create/Update
GORM will auto-save associations and its reference using Upsert when creating/updating a record.
user := User{ |
If you want to update associations’s data, you should use the FullSaveAssociations
mode:
db.Session(&gorm.Session{FullSaveAssociations: true}).Updates(&user) |
Skip Auto Create/Update
To skip the auto save when creating/updating, you can use Select
or Omit
, for example:
user := 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
db.Omit("Languages").Create(&user)
Select/Omit Association fields
user := User{ |
Association Mode
Association Mode contains some commonly used helper methods to handle relationships
// Start Association Mode |
Find Associations
Find matched associations
db.Model(&user).Association("Languages").Find(&languages) |
Find associations with conditions
codes := []string{"zh-CN", "en-US", "ja-JP"} |
Append Associations
Append new associations for many to many
, has many
, replace current association for has one
, belongs to
db.Model(&user).Association("Languages").Append([]Language{languageZH, languageEN}) |
Replace Associations
Replace current associations with new ones
db.Model(&user).Association("Languages").Replace([]Language{languageZH, languageEN}) |
Delete Associations
Remove the relationship between source & arguments if exists, only delete the reference, won’t delete those objects from DB.
db.Model(&user).Association("Languages").Delete([]Language{languageZH, languageEN}) |
Clear Associations
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() |
Batch Data
Association Mode supports batch data, e.g:
// Find all roles for all users |
Delete Association Record
By default, Replace
/Delete
/Clear
in gorm.Association
only delete the reference,
that is, set old associations’s foreign key to null.
You can delete those objects with Unscoped
(it has nothing to do with ManyToMany
).
How to delete is decided by gorm.DB
.
// Soft delete |
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 |
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
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 |
constraint | Relations constraint, e.g: OnUpdate ,OnDelete |