Save
Save routes an insert or an update by primary key — including element-wise routing for slices — and FullSaveAssociations extends the idea to relations.
Save decides between Create and Updates from the primary key. Zero
key → insert; set key → update:
// Save inserts value when its primary key is zero and updates it
// otherwise. For a slice each element is handled on its own — rows with a
// primary key are updated by that key, rows without one are inserted —
// because routing the whole slice to Updates would fail with "Updates
// expects a map or struct" (GORM #7540).Single struct
if err := db.Save(&crudUser{ID: 1, Name: "saved"}).Error; err != nil {
panic(err)
}
// the row with id 1 now has name 'saved'The routing is precise:
- Primary key zero — the struct goes to
Create: full INSERT, hooks and association cascade included. - Primary key set — the struct goes to
db.Where("id = ?", id).Updates(value). That means it behaves like a struct update: only non-zero fields are written, the primary key is never part of the SET list,UpdatedAtis bumped, and theBeforeUpdate/AfterUpdatehooks run.
Save is not a full-row overwrite
Unlike GORM, a Save on a row with a set primary key does not write every
column — zero fields are skipped, exactly like a struct Updates. A save
whose struct has a primary key and nothing else fails with
ErrInvalidValue ("no values to update"). Use a map Updates when you
need to write zeros.
On models with a composite primary key, the match uses the full key tuple
(a = ? AND b = ?), and any zero key field routes back to insert
semantics.
Slices: element-wise routing
Save over a slice handles each element on its own, by its primary key —
rows with a set key are updated, rows without one are inserted:
// TestAuditSaveSliceWithPKs: Save over a slice of persisted models must
// update every row, not fail on the slice type. GORM #7540 class.// `all` is a slice fetched earlier and mutated locally:
all[0].Name = "a2"
if err := db.Save(&all).Error; err != nil {
panic(err)
}
// row 1 was updated in place; no second insert happenedEach element pays its own statement — there is no batching on this path.
For bulk inserts use Create with CreateBatchSize
(create & batch inserts).
FullSaveAssociations
By default, association saves on Create treat a child that already carries
a primary key carefully (no duplicate insert). With
FullSaveAssociations, associated records that have a primary key are
updated in place instead:
// FullSaveAssociations makes association saves use Save (not Create) for
// records that already carry a primary key.frank := &relUser{Name: "frank", Tags: []relTag{{ID: 1, Name: "renamed"}}}
if err := db.Session(&Session{FullSaveAssociations: true}).Create(frank).Error; err != nil {
panic(err)
}
// tag 1 is renamed; the tag count is unchangedIt is available on Config (session-wide) and on Session (per
derivative). See
association callbacks for the
save order.
Save, Create, or Updates?
| You have | Use |
|---|---|
| A new struct (no key yet) | Create — same as Save, but explicit. |
| A struct round-tripped from a query, changed fields | Save — concise insert-or-update routing. |
| Specific columns to write, including zeros | Updates(map[string]any{...}) with a Where. |
| A slice mixing new and known rows | Save — routes each element by key. |
| A bulk insert | Create with CreateBatchSize(n). |