FAQ
Honest answers to the questions that come up most — all verified against the source.
Can I use raw SQL?
Yes. db.Exec(sql, args...) runs DDL/DML with bound arguments and reports
Error/RowsAffected on the returned *DB. For SELECTs, Row() returns a
raw *sql.Row, Rows() returns a *sql.Rows (you close it), and
Scan(dest) scans the built query into a struct, map, or scalar. ToSQL
renders any chain into an interpolated display string without executing it.
tx := db.Exec("UPDATE users SET active = ?", true)
if tx.Error != nil { /* ... */ }How do connections and pooling work?
fusion-orm rides the standard database/sql pool. Open asks the dialector
to initialize the pool (fusion.go:199), pings it once unless
DisableAutomaticPing is set, and every statement runs through that
ConnPool. db.DB() unwraps wrappers (including the prepared-statement
cache) and hands you the *sql.DB, so you can tune SetMaxOpenConns and
friends as usual — pooling itself is database/sql's, not ours.
pool, err := db.DB()Can I use it with an existing database?
Yes. AutoMigrate is opt-in — nothing touches your schema unless you call
it. For tables that already exist, use Model/Table against the live
schema and let tags describe the columns you care about; the tests
themselves create tables with raw Exec DDL and then query them with
Model (crud_test.go:39). Table("name") overrides the derived table
name when the mapping is non-obvious.
Why no MySQL or MSSQL?
They are not shipped. The dialect layer is a Dialector interface plus a
feature bitset (~35 capability flags), so adding a dialect is mostly
declaring flags and identifier quoting (Dialects) — but
no MySQL/MSSQL dialect exists in this repository today, and we won't
document one until it does.
Generics vs the reflection API — which should I use?
Whichever reads better to you. fusion.G[T] is a thin typed facade over
the same chain-and-clause engine; a parity test asserts the generated SQL
is byte-identical between both APIs (generics_test.go:71). The reflection
API exposes a few more finishers (Row, Rows, FirstOrInit on raw
values); the generics API returns values instead of filling your struct
and is checked at compile time against ChainInterface[T]/ExecInterface[T].
Do prepared statements help?
Often, if you execute the same statement shapes repeatedly: enable
Config.PrepareStmt and every rendered SQL becomes an LRU-cached
(prepare-once) statement, with optional TTL expiry. The cache is
transaction-aware and purges on driver.ErrBadConn; AutoMigrate
invalidates it because DDL invalidates prepared statements
(prepared_stmt_test.go:158). For one-off or highly varied queries the
cache just churns — leave it off there.
db, err := fusion.Open(sqlite.Open("app.db"), &fusion.Config{PrepareStmt: true})How do I see the SQL?
Four ways, from most to least surgical:
ToSQL(func(tx *DB) *DB { ... })— renders one chain, interpolated for display (example_test.go:117).Session(&Session{DryRun: true})— captures SQL and vars onStatementinstead of executing (fusion_test.go:115).Debug()— traces every statement on that derivative at Info level.- The
Loggerconfig field — bring your own, or use the slog adapter (Hooks & observability).
Is soft delete on by default?
No. It activates only when the model has a DeletedAt field (any field
with that name, or an explicit softdelete tag — schema/field.go:212).
Plain structs hard-delete. Models embedding fusion.Model/UUIDModel get
it through the embedded DeletedAt.
How does fusion-orm compare to gorm and bun?
Deliberate blend: bun's engine (byte-slice SQL building, feature flags, precompiled field closures) plus gorm's API (fluent chaining, callbacks, hooks, sessions). There is a side-by-side capability table in Comparison, and each row is verified against this repo — see Why fusion-orm.
Can I hook every query?
Yes — QueryHook observes every executed statement: BeforeQuery may
replace the context used for the I/O call, AfterQuery receives the
completed event (SQL, vars, duration, error). db.QueryHook(hook) attaches
one to a derivative, and SlogQueryHook ships in-tree
(query_hook_test.go:63). For CRUD-specific logic, model hooks and
registerable callbacks are the other two layers
(Callbacks & plugins).
type QueryHook interface {
BeforeQuery(ctx context.Context, event *QueryEvent) context.Context
AfterQuery(ctx context.Context, event *QueryEvent)
}What about Group and Having?
Not implemented. Group() and Having() do not exist on *DB today —
there is no clause.Group builder and no GROUP BY slot in the build
order. Grouped aggregation is done with Select aggregates plus Scan;
global aggregates work fine that way. The honest write-up, including a
workaround that looks plausible but fails, is
GROUP BY & HAVING.
Does it support composite primary keys?
Yes, with the v0.4.0 semantics: First/Last order by the full PK tuple,
maps address individual columns, and a scalar id on a composite-key model
returns ErrInvalidValue instead of silently matching every sibling row
(composite_pk_test.go:59). FindInBatches paginates composite keys with
row-value keysets where the dialect supports them. Full rules:
Composite primary keys.