Session is a lightweight configuration overlay applied on top of an existing *DB. Call db.Session(&gorm.Session{...}) to get a new *DB that inherits the parent’s configuration but with the overrides you specify.
Because Session returns a new *DB without modifying the original, it is safe to use from multiple goroutines.
Type definition
Fields
Generate SQL without executing it. The generated SQL is stored in
db.Statement.SQL and can be retrieved with db.ToSQL.Enable prepared statement caching for all queries made through this session. Repeated identical queries skip statement parsing on the server.
When
true, the returned *DB starts with a clean statement — no inherited conditions, model, or clauses. Equivalent to a fresh db but sharing the same connection pool and configuration.When
true, Session calls getInstance() on the returned *DB, materialising the statement immediately. Used internally by some GORM operations.When
true, all before/after hooks (callbacks) are skipped for operations on this session. Useful when you want raw database operations without lifecycle callbacks.Override the global
Config.SkipDefaultTransaction setting for this session, preventing GORM from wrapping individual create/update/delete operations in a transaction.Override the global
Config.DisableNestedTransaction setting for this session. When true, calling Transaction inside an existing transaction runs the callback on the outer transaction without creating a savepoint.Override the global
Config.AllowGlobalUpdate setting for this session. When true, updates and deletes without WHERE conditions are permitted.Override the global
Config.FullSaveAssociations setting for this session. When true, GORM performs a full upsert of nested associations on save.When
true, the Unscoped flag is propagated to all nested statements (preloads, association queries) within this session.When
true, SELECT statements list every column explicitly rather than using SELECT *.A context to attach to all statements in this session. Cancellations and deadlines from this context propagate to the underlying database driver.
Override the logger for this session only.
nil means inherit the parent logger.Override the function used to generate the current timestamp for this session.
Default batch size for
Create operations in this session. A value of 0 means no batching (unless Config.CreateBatchSize is set globally).Using db.Session
Session creates a new *DB by cloning the current config and selectively applying fields from *Session. The original *DB is not modified.
Using db.WithContext
WithContext is a convenience wrapper around Session that sets only the Context field.