Skip to Content
fusion-orm
API Reference

Finisher methods

Every executing method on *DB — exact signatures from finisher_api.go, exec.go, and soft_delete.go, plus the two-field result contract.

Finishers trigger the callback pipeline: SQL is built and executed, rows scan back into dest, and the derivative *DB reports the outcome.

The two-field contract

Every finisher returns the derivative *DB, which reports through exactly two fields:

// Error is the single error slot for the chain (see AddError).
Error error
// RowsAffected is set by finishers that execute DML.
RowsAffected int64
res := db.Model(&User{}).Where("id = ?", 1).Update("name", "Ada")
if res.Error != nil { /* branch on sentinels with errors.Is */ }
res.RowsAffected // rows written

Errors accumulate on a single slot with wrap-chain semantics — the first error stays the errors.Is head, later errors append with ; %w. See Error sentinels.

CRUD

Create(value any)

func (db *DB) Create(value any) *DB

Inserts value (a struct or slice of structs). Primary keys and timestamps are backfilled onto the struct.

user := User{Name: "Ada"}
db.Create(&user)

Save(value any)

func (db *DB) Save(value any) *DB

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.

Update(column string, value any)

func (db *DB) Update(column string, value any) *DB

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

Updates(values any)

func (db *DB) Updates(values any) *DB

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

db.Model(&User{}).Where("id = ?", 1).Updates(map[string]any{"name": "Ada"})

UpdateColumn(column string, value any)

func (db *DB) UpdateColumn(column string, value any) *DB

Updates without hooks or automatic timestamps (single column).

UpdateColumns(values any)

func (db *DB) UpdateColumns(values any) *DB

Updates without hooks or automatic timestamps.

Delete(value any, conds ...any)

func (db *DB) Delete(value any, conds ...any) *DB

Removes matching rows. A WHERE clause (or value with a primary key) is required unless AllowGlobalUpdate is set. Models with a soft-delete field are rewritten to an UPDATE instead.

db.Delete(&User{}, 1)
db.Model(&User{}).Where("active = ?", false).Delete(nil)

Restore(value any)

func (db *DB) Restore(value any) *DB

Undeletes rows that were soft-deleted. value carries the primary keys to restore; the model must have a soft-delete field and a primary key.

Queries

Find(dest any, conds ...any)

func (db *DB) Find(dest any, conds ...any) *DB

Scans all matching rows into dest. A miss is not an error — dest stays empty and Error stays nil.

var users []User
db.Find(&users)

First(dest any, conds ...any)

func (db *DB) First(dest any, conds ...any) *DB

Scans the first row ordered by primary key; a miss returns ErrRecordNotFound. Ordering uses the full primary-key tuple, so First/Last are deterministic on composite keys.

Take(dest any, conds ...any)

func (db *DB) Take(dest any, conds ...any) *DB

Scans the first row without implicit ordering. Like First, a miss returns ErrRecordNotFound.

Last(dest any, conds ...any)

func (db *DB) Last(dest any, conds ...any) *DB

Scans the last row ordered by primary key descending.

Count(count *int64)

func (db *DB) Count(count *int64) *DB

Stores the number of matching rows in count. ORDER BY is dropped (it is invalid next to COUNT(*) on PostgreSQL); GROUP BY/HAVING are left intact.

var n int64
db.Model(&User{}).Where("active = ?", true).Count(&n)

Pluck(column string, dest any)

func (db *DB) Pluck(column string, dest any) *DB

Scans one column into dest (a slice or scalar).

FindInBatches(dest any, batchSize int, fc func(tx *DB, batch int) error)

func (db *DB) FindInBatches(dest any, batchSize int, fc func(tx *DB, batch int) error) *DB

Queries in primary-key order, calling fc for each batch. The model needs a primary key; composite keys paginate with row-values, which requires the dialect's RowValues feature.

FirstOrInit(dest any, conds ...any)

func (db *DB) FirstOrInit(dest any, conds ...any) *DB

Finds the first matching row or initializes dest from the conditions and Attrs/Assign values without writing to the database.

FirstOrCreate(dest any, conds ...any)

func (db *DB) FirstOrCreate(dest any, conds ...any) *DB

Finds the first matching row or inserts an initialized dest.

db.Where(&User{Name: "Ada"}).Attrs(User{Active: true}).FirstOrCreate(&user)

Raw access

Scan(dest any)

func (db *DB) Scan(dest any) *DB

Runs the accumulated query into dest.

Row()

func (db *DB) Row() *sql.Row

Builds the query and returns the first row for manual scanning. If the query could not be built, a *sql.Row cannot carry a custom error, so the returned row's Scan fails with a deterministic error and the build error is surfaced on the receiver.

row := db.Model(&User{}).Select("name").Where("id = ?", 1).Row()
var name string
row.Scan(&name)

Rows()

func (db *DB) Rows() (*sql.Rows, error)

Builds the query through the row processor and returns the result set; the caller must close it.

Exec(sql string, args ...any)

func (db *DB) Exec(sql string, args ...any) *DB

Executes raw SQL with bound arguments and returns a derived instance carrying Error and RowsAffected. The statement is traced through the session logger; with Config.DryRun set the SQL and args are captured on Statement instead of being executed (see ToSQL).

tx := db.Exec("UPDATE users SET active = ?", true)
if tx.Error != nil { ... }

Find vs First/Take/Last

First, Take, and Last return ErrRecordNotFound when nothing matches. Find returns an empty slice and a nil error instead.

Next

On this page