CRUD
Create, query, update, and delete records — the full finisher surface, with the SQL each call renders.
Every CRUD operation is a finisher: a method that triggers the create,
query, update, or delete callback chain and reports through exactly two
fields — Error and RowsAffected. Chainables prepare the statement;
finishers execute it.
// Create inserts value (a struct or slice of structs).db.Create(&user) // INSERT
db.Find(&users, "age > ?", 18) // SELECT
db.Model(&User{}).Where("id = ?", 1).Update("age", 31) // UPDATE
db.Delete(&user, user.ID) // DELETEThe pages in this section cover each family in turn, with the rendered SQL for SQLite and PostgreSQL side by side.
What every finisher shares
- Derivative chaining — a finisher calls
getInstance()first, so it never mutates the receiver (see chaining & copy-on-write). - Hooks — model hooks run inside the default callback chains:
BeforeCreate/AfterCreate,BeforeUpdate/AfterUpdate,BeforeDelete/AfterDelete,AfterFind(see model hooks). - The error slot — nothing panics; the first failure is recorded on
Errorand the chain stops early, while finalizers (commit/rollback) always run. - Feature-gated SQL — dialects declare capabilities as a bitset, so the
same call renders
RETURNING "id"on both built-in dialects and falls back to a driver backfill on dialects without the flag. Divergences are called out per page.
Guardrails
Two guards apply to every write:
- Global-write guard —
Update/Updates/Deletewithout a WHERE clause (or a value carrying a primary key) fail withfusion.ErrMissingWhereClauseunless you opt in viaSession(&fusion.Session{AllowGlobalUpdate: true}). - Soft delete — models with a soft-delete column rewrite
DELETEinto anUPDATEof that column, and every query filters deleted rows (see soft delete).
In this section
Create & batch inserts
Create over structs and slices, CreateBatchSize, timestamp and
primary-key backfill.
Querying records
Find, First/Take/Last, inline conditions, FindInBatches,
FirstOrInit/FirstOrCreate.
Updates
Update/Updates, struct vs map semantics, the timestamp bump,
UpdateColumn(s), the global-update guard.
Delete
Deletes by key, struct, and condition; soft-delete rewriting and
Unscoped.
Save
Insert-or-update routing, slice semantics, FullSaveAssociations.
Upserts & OnConflict
clause.OnConflict through Clauses(), rendered SQL, dialect support.
Count, Pluck & Scan
Aggregates, column extraction, scanning into maps and scalars, raw rows.