Skip to Content
fusion-orm
CRUD

Upserts & OnConflict

clause.OnConflict through Clauses() — rendered SQL, dialect feature flags, and FirstOrCreate as the application-level alternative.

An upsert is a single INSERT ... ON CONFLICT statement. fusion-orm does not give it a dedicated finisher — you attach a clause.OnConflict clause to an ordinary Create through Clauses():

// OnConflict renders ON CONFLICT (cols) DO NOTHING / DO UPDATE SET ...
type OnConflict struct {
	Columns       []string
	DoNothing     bool
	UpdateColumns []string
	UpdateAll     bool
}
FieldEffect
ColumnsThe conflict target — the quoted column list in ON CONFLICT (...). Empty renders a targetless ON CONFLICT with no parentheses.
DoNothingRenders DO NOTHING; takes precedence over the update fields.
UpdateColumnsRenders DO UPDATE SET with the listed columns taken from EXCLUDED.
UpdateAllExpands to every non-primary-key column, with the primary key as the conflict target when Columns is empty.

The golden rendering

The clause builder is covered by dialect-level golden tests:

"on conflict update", OnConflict{Columns: []string{"email"}, UpdateColumns: []string{"name"}},
`ON CONFLICT ("email") DO UPDATE SET "name" = EXCLUDED."name"`,

The same bytes render on both built-in dialects — SQLite and PostgreSQL differ only in placeholder style. Clauses build in canonical order (insert → values → on_conflict → returning), so an upsert with primary-key backfill reads:

INSERT INTO "users" ("name", "age", "email", "created_at", "updated_at")
VALUES ('Ada', 30, 'ada@example.com', '2026-11-28 10:00:00', '2026-11-28 10:00:00')
ON CONFLICT ("email") DO UPDATE SET "name" = EXCLUDED."name" RETURNING "id"

DoNothing swaps the action:

INSERT INTO "users" ("name", "email") VALUES ('Ada', 'ada@example.com')
ON CONFLICT ("email") DO NOTHING

Full-row upserts with UpdateAll

UpdateAll upserts every non-primary-key column; when no target is given, the primary key becomes the conflict target. This is exercised against a live SQLite engine:

upsert := clause.OnConflict{UpdateColumns: []string{"name", "age"}}
if err := db.Clauses(upsert).Create(&upsertUser{ID: 1, Name: "a", Age: 10}).Error; err != nil {
	panic(err)
}
// second call with different values updates the row in place

if err := db.Clauses(clause.OnConflict{UpdateAll: true}).
	Create(&upsertUser{ID: 1, Name: "c", Age: 12}).Error; err != nil {
	panic(err)
}

The invalid-SQL guard

Two failure shapes are structurally impossible:

  • ON CONFLICT () — an empty target renders without parentheses.
  • A dangling DO UPDATE SET — when there is nothing to update (no DoNothing, no UpdateColumns), the whole clause is skipped instead of emitting a broken suffix.
// Nothing to update: skip the clause rather than emit invalid SQL
// such as "ON CONFLICT ()" or a dangling "DO UPDATE SET"
// (GORM #7346 empty-target class).
// TestAuditOnConflictRendersNoEmptyTarget: ON CONFLICT with update columns
// but no conflict target must not render the invalid `ON CONFLICT ()`.

This landed in v0.3.0 alongside UpdateAll (changelog).

Dialect support

The clause is feature-gated: it renders only on dialects that declare feature.InsertOnConflict.

func (o OnConflict) Build(b Builder) {
	if !b.Dialector().Features().Has(feature.InsertOnConflict) {
		return
	}
	// ...
}

Both in-tree dialects declare it — PostgreSQL (dialect/postgres) and SQLite (dialect/sqlite), whose ON CONFLICT requires SQLite 3.35 or newer (the modernc.org/sqlite driver bundled here satisfies that).

MySQL-style flags exist, no MySQL dialect ships

The feature bitset also defines InsertOnDuplicateKey and InsertIgnore (dialect/feature/feature.go). Neither built-in dialect declares them, and no MySQL dialector ships in-tree — they are reserved for third-party dialects. On a dialect without the flag, OnConflict is silently dropped and your Create behaves as a plain insert.

FirstOrCreate: the application-level alternative

When you cannot rely on ON CONFLICT — portability, or logic that needs the existing row — FirstOrCreate expresses the same intent at the application level: find the row, insert it when missing.

// FirstOrCreate finds the first matching row or inserts an initialized dest.

The trade-offs differ: FirstOrCreate costs two round trips and has a find-then-insert race under concurrency (guard the column with a unique constraint and retry on the translated constraint error), while OnConflict is one atomic statement. FirstOrCreate also never updates a found row — see querying records.

Next

On this page