Auto Create/Update

Upsert when creating/updating a record.

user := User{
Name: "jinzhu",
BillingAddress: Address{Address1: "Billing Address - Address 1"},
ShippingAddress: Address{Address1: "Shipping Address - Address 1"},
Emails: []Email{
{Email: "jinzhu@example.com"},
{Email: "jinzhu-2@example.com"},
},
Languages: []Language{
{Name: "ZH"},
{Name: "EN"},
},
}

db.Create(&user)
// BEGIN TRANSACTION;
// INSERT INTO "addresses" (address1) VALUES ("Billing Address - Address 1"), ("Shipping Address - Address 1") ON DUPLICATE KEY DO NOTHING;
// INSERT INTO "users" (name,billing_address_id,shipping_address_id) VALUES ("jinzhu", 1, 2);
// INSERT INTO "emails" (user_id,email) VALUES (111, "jinzhu@example.com"), (111, "jinzhu-2@example.com") ON DUPLICATE KEY DO NOTHING;
// INSERT INTO "languages" ("name") VALUES ('ZH'), ('EN') ON DUPLICATE KEY DO NOTHING;
// INSERT INTO "user_languages" ("user_id","language_id") VALUES (111, 1), (111, 2) ON DUPLICATE KEY DO NOTHING;
// COMMIT;

db.Save(&user)

If you want to update associations’s data, you should use the FullSaveAssociations mode:

db.Session(&gorm.Session{FullSaveAssociations: true}).Updates(&user)
// ...
// INSERT INTO "addresses" (address1) VALUES ("Billing Address - Address 1"), ("Shipping Address - Address 1") ON DUPLICATE KEY SET address1=VALUES(address1);
// INSERT INTO "users" (name,billing_address_id,shipping_address_id) VALUES ("jinzhu", 1, 2);
// INSERT INTO "emails" (user_id,email) VALUES (111, "jinzhu@example.com"), (111, "jinzhu-2@example.com") ON DUPLICATE KEY SET email=VALUES(email);
// ...

Skip Auto Create/Update

Select/Omit Association fields

user := User{
Name: "jinzhu",
BillingAddress: Address{Address1: "Billing Address - Address 1", Address2: "addr2"},
ShippingAddress: Address{Address1: "Shipping Address - Address 1", Address2: "addr2"},
}

// 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)

db.Omit("BillingAddress.Address2", "BillingAddress.CreatedAt").Create(&user)

Association Mode

Find Associations

Append Associations

Replace Associations

Delete Associations

Clear Associations

Count Associations

Batch Data

Delete Association Record

Delete with Select

Association Tags

Overview Declaring Models Connecting to Database CRUD Interface Create Query Advanced Query Update Delete Raw SQL & SQL Builder Associations Belongs To Has One Has Many Many To Many Association Mode Preloading (Eager Loading) Tutorials Context Error Handling Method Chaining Session Hooks Transactions Migration Logger Generic Database Interface Performance Customize Data Types Scopes Conventions Settings Advanced Topics Database Resolver Sharding Serializer Prometheus Hints Indexes Constraints Composite Primary Key Security GORM Config Write Plugins Write Driver ChangeLog Community Contribute Translate current site Getting Started Overview Declaring Models Connecting to Database CRUD Interface Create Query Advanced Query Update Delete Raw SQL & SQL Builder Associations Belongs To Has One Has Many Many To Many Association Mode Preloading (Eager Loading) Tutorials Context Error Handling Method Chaining Session Hooks Transactions Migration Logger Generic Database Interface Performance Customize Data Types Scopes Conventions Settings Advanced Topics Database Resolver Sharding Serializer Prometheus Hints Indexes Constraints Composite Primary Key Security GORM Config Write Plugins Write Driver ChangeLog Community Contribute Translate current site English
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