Skip to Content
fusion-orm
CRUD

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

The 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 Error and 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/Delete without a WHERE clause (or a value carrying a primary key) fail with fusion.ErrMissingWhereClause unless you opt in via Session(&fusion.Session{AllowGlobalUpdate: true}).
  • Soft delete — models with a soft-delete column rewrite DELETE into an UPDATE of that column, and every query filters deleted rows (see soft delete).

In this section

On this page