Skip to Content
fusion-orm
Core Concepts

Sessions, context & Debug

Per-call configuration with Session, WithContext, and Debug — and what each one clones.

Session, WithContext, and Debug are the three ways to configure a single chain without touching the root. Each returns a derivative with a copied Config.

The Session struct

// Session returns a derivative configured per config. Flags on config are
// one-way overrides onto a copied Config; the receiver's Config is never
// mutated. With NewDB false (default) the derivative inherits the current
// chain's statement; with Context set the statement is cloned first so the
// new context cannot leak back.
func (db *DB) Session(config *Session) *DB
FieldTypeWhat it does
DryRunboolTurns the derivative into a SQL-capturing session — statements assemble and record SQL/vars but never execute.
NewDBboolResets the chain: the derivative starts with a fresh Statement instead of inheriting the receiver's clauses.
InitializedboolForces the derivative through getInstance immediately.
SkipDefaultTransactionboolOverrides the config flag for this session only.
DisableNestedTransactionboolOverrides the config flag for this session only.
AllowGlobalUpdateboolPermits no-WHERE Update/Delete for this session only — the per-call escape hatch for the guard.
Contextcontext.ContextReplaces the derivative's execution context.
Loggerlogger.InterfaceReplaces the derivative's logger (this is how Debug works).
NowFuncfunc() time.TimeReplaces the derivative's timestamp source.
FullSaveAssociationsboolSaves associated records with Save instead of Create on this derivative.

All overrides are one-way: they set flags that are currently false; they never unset flags set on the root config.

WithContext

WithContext propagates ctx to every statement on the derivative — every I/O call made through the statement's context honors cancellation and deadlines.

// WithContext returns a derivative that runs every statement under ctx.
ctx, cancel := context.WithTimeout(context.Background(), 2*time.Second)
defer cancel()

var users []User
err := db.WithContext(ctx).Model(&User{}).Where("active = ?", true).Find(&users).Error

A nil context becomes context.TODO() — never a panic.

Debug

Debug returns a derivative whose logger is the session's logger switched to Info level, so every SQL trace shows up.

// Debug returns a derivative that logs every SQL trace at Info level.
db.Debug().Model(&User{}).Where("name = ?", "Ada").Find(&users)

Because it's a derivative, only this chain is verbose — the root keeps its normal level.

Patterns

Capture SQL for logging or tests without a round-trip:

tx := db.Session(&fusion.Session{DryRun: true}).
	Model(&User{}).
	Where("name = ?", "Ada")
// tx.Statement.SQL / tx.Statement.Vars now hold what would run

Allow a one-off global update while the root stays guarded:

err := db.Session(&fusion.Session{AllowGlobalUpdate: true}).
	Model(&User{}).
	Update("active", false).Error

Get a fresh statement that ignores inherited clauses:

fresh := db.Session(&fusion.Session{NewDB: true})

What a derivative inherits

With NewDB: false (the default), a derivative inherits the receiver's statement — clauses, model, and all. Use NewDB: true when you want a pristine chain that shares only the pool, caches, and callback processors.

Next

On this page