Skip to Content
fusion-orm
CRUD

Updates

Update and Updates, struct vs map semantics, automatic timestamps, UpdateColumn(s), and the global-update guard.

The update family writes columns to matching rows. All of them funnel into one clause — SET — and one callback chain.

Update — one column

// Update sets one column on matching rows. A WHERE clause (or a model with
// a primary key) is required unless AllowGlobalUpdate is set.

Update(column, value) is a thin wrapper over Updates with a one-key map. RowsAffected reports how many rows matched.

ok := db.Model(&crudUser{}).Where("name = ?", "alice").Update("age", 31)
// ok.RowsAffected == 1

Updates — map or struct

// Updates sets columns from a map or struct. A WHERE clause (or a model
// with a primary key) is required unless AllowGlobalUpdate is set.

The two shapes differ in one important way:

  • Map — every key becomes an assignment, zero values included. An unknown column key adds ErrInvalidValue to the chain.
  • Struct — only non-zero fields become assignments; primary keys are always skipped. You cannot set a column to its zero value with a struct.
updates := db.Model(&crudUser{}).
	Where("name = ?", "bob").
	Updates(map[string]any{"age": 26, "email": "new@example.com"})

Struct updates skip zero fields

Updates(User{Name: "x", Age: 0}) will not touch age. To write zeros, use a map — or UpdateColumn, which takes one column and any value.

An update whose assignments end up empty — Updates(nil), or a struct with only zero and primary-key fields — fails with fusion.ErrInvalidValue ("no values to update"):

{
	name: "empty update values",
	want: ErrInvalidValue,
	run:  func() error { return db.Model(&errPathUser{}).Updates(nil).Error },
},

UpdatedAt is bumped automatically

When the model has an auto-update-time field (UpdatedAt), the update callback appends SET "updated_at" = ? — unless you are updating that column explicitly already. The timestamp comes from Config.NowFunc.

before := alice.UpdatedAt
_ = db.Model(&crudUser{}).Where("name = ?", "alice").Update("age", 34)
_ = db.First(&alice, "name = ?", "alice")
// alice.UpdatedAt is now after `before`

UpdateColumn and UpdateColumns skip both the hooks and the automatic timestamps:

// UpdateColumn is Updates without hooks or automatic timestamps.
// UpdateColumns is Updates without hooks or automatic timestamps.
res := db.Model(&crudUser{}).Where("name = ?", "alice").UpdateColumn("age", 33)

They set Statement.SkipHooks and Statement.SkipTimestamps and delegate to Updates — the WHERE clause and guard behavior are unchanged.

The global-update guard

An update with no WHERE clause is rejected:

global := db.Model(&crudUser{}).Update("name", "everyone")
// errors.Is(global.Error, fusion.ErrMissingWhereClause) == true

Three things satisfy the guard:

  1. an explicit Where/Or condition on the chain;
  2. struct-shaped destination conditions — finishers that take a model value (Save, Delete) put it in the statement's destination slot, where its non-zero fields, primary key included, become the condition;
  3. AllowGlobalUpdate.

Updates on a bare Model never sets a destination, so it always needs an explicit Where — even when the model instance carries a primary key.

allowed := db.Session(&Session{AllowGlobalUpdate: true}).
	Model(&crudUser{}).Update("name", "x")
// updates all rows; RowsAffected == 3

AllowGlobalUpdate is available on Config (session-wide) and on Session (per derivative, copy-on-write).

What runs

The update chain mirrors create:

// The update chain, in order: begin_tx, before_update,
// save_before_associations, update, save_after_associations,
// after_update, commit_or_rollback.

Model hooks BeforeUpdate/AfterUpdate run around the statement (skipped by UpdateColumns), association saves cascade on both sides (see association callbacks), and the implicit transaction is skipped for provably single-statement writes just as on create.

Rendered for the Updates call above:

UPDATE "crud_users" SET "age" = 26, "email" = 'new@example.com',
	"updated_at" = '2026-11-28 10:00:00' WHERE name = 'bob'

Returning modified rows

clause.Returning adds a RETURNING list to the UPDATE, gated on the dialect's Returning feature flag:

db.Model(&crudUser{}).
	Clauses(clause.Returning{Columns: []string{"id", "updated_at"}}).
	Where("id = ?", 1).
	Update("name", "ada")
UPDATE "crud_users" SET "name" = 'ada',
	"updated_at" = '2026-11-28 10:00:00+00:00'
WHERE id = 1 RETURNING "id", "updated_at"

Update bumps updated_at like any other update; use UpdateColumn if you do not want the timestamp in the statement.

An empty clause.Returning{} renders nothing — name the columns explicitly. On a dialect without feature.Returning the clause is likewise a no-op: the update still runs, just without returned rows. See Advanced query for the read side.

Next

On this page