*gorm.Config to gorm.Open. Every field has a safe default, so you only need to set the fields you want to change.
Config fields
Transaction behavior
By default GORM wraps each
Create, Update, and Delete call in a transaction to protect data integrity. Set this to true to skip that transaction and execute statements directly, which can improve throughput when you are managing transactions yourself.Sets a default timeout for transactions started by GORM. When non-zero, a context with this deadline is automatically applied to each transaction.
Sets a default timeout for the context attached to each database operation. Useful as a global safety net for runaway queries.
When
true, GORM will not use SAVEPOINT inside an existing transaction. Instead, the nested Transaction() call reuses the outer transaction directly.Naming and schema
Controls how GORM maps struct names and field names to table and column names. Pass a custom implementation of
schema.Namer to apply your own conventions.Associations
When
true, GORM upserts associated records during Create and Save operations. When false (default), only the foreign key on the owner record is updated.Logging
The logger used by GORM to emit SQL traces, slow query warnings, and errors. Replace with any implementation of See Logger for full details.
logger.Interface.Time
The function GORM calls to generate timestamps for
CreatedAt, UpdatedAt, and soft-delete DeletedAt fields. Override it to use UTC, a mock time in tests, or a custom precision.Debugging
When
true, GORM generates SQL statements but does not execute them. Useful for inspecting generated SQL or in tests.Prepared statements
Enables the prepared-statement cache globally. All queries are compiled once and reused from an in-process cache, reducing parse overhead on subsequent executions.
Sets the maximum number of prepared statements held in the LRU cache. Once the cache is full, the least recently used entry is evicted. The default allows an unlimited number of cached statements.
How long a prepared statement remains in the cache before being evicted. After the TTL expires the statement is re-prepared on the next use.
Connection
By default, GORM pings the database after
Open to verify connectivity. Set this to true to skip that ping, for example when you are using a lazy connection pool.Lets you supply a custom connection pool (anything implementing
ConnPool) instead of the dialector’s default pool. Useful when sharing a *sql.DB instance across multiple GORM instances.Migrations
When
true, GORM does not create foreign key constraints during AutoMigrate or CreateTable. Helpful for databases that have limited foreign key support, or when you prefer to manage constraints out-of-band.When
true, GORM ignores all relationship fields (foreign keys, join tables) during migration, so only the model’s own columns are created or altered.Queries
GORM returns an error when you call
Update or Delete without a WHERE clause (to prevent accidental mass writes). Set this to true to allow those operations.When
true, SELECT statements are generated with explicit column names (SELECT id, name, ...) rather than SELECT *. This avoids issues with added columns and can make query plans more predictable.When non-zero,
Create splits a slice of records into batches of this size. This reduces memory pressure and avoids exceeding per-query parameter limits for large inserts.Errors
When
true, GORM translates database-specific errors (such as unique constraint violations) into GORM’s own error types like gorm.ErrDuplicatedKey. The dialector must implement ErrorTranslator for this to have any effect.Scoping
When
true, an Unscoped() call on the parent session propagates to all nested associations, so soft-deleted records are included throughout the entire query tree.Extensibility
A map of clause names to custom builder functions. Use this to override how GORM renders specific SQL clauses for a dialector or a particular use case.
The database driver implementation. Normally passed as the first argument to
gorm.Open, but you can also embed it in Config.Map of registered plugins, keyed by plugin name. Plugins are initialized after the database connection is established. Prefer
db.Use(plugin) over setting this directly.Session
ASession lets you create a new *gorm.DB that shares the underlying connection pool but has overridden settings for a single chain of calls. The original db is not modified.
Session fields
Generates SQL without executing it for this session.
Enables the prepared-statement cache for this session. Shares the same statement cache as the parent
db.When
true, the new session starts with a clean statement (no previously applied conditions or clauses carry over).When
true, the returned *DB is immediately initialized rather than lazily on the next operation. Rarely needed outside of plugin development.When
true, all BeforeCreate, AfterCreate, and other model hook methods are skipped for this session.Skips the implicit transaction for single-statement writes in this session.
Disables
SAVEPOINT usage inside transactions for this session.Permits
UPDATE/DELETE without a WHERE clause in this session.Upserts associations in this session.
Propagates
Unscoped() to nested associations in this session.Generates explicit column lists instead of
SELECT * for this session.Attaches a context to every operation in this session. The context controls cancellation, deadlines, and propagated values.
Overrides the logger for this session only.
Overrides the timestamp function for this session.
Overrides the batch size for
Create in this session.Shorthands
db.WithContext is a convenience wrapper for the common pattern of attaching a context:
db.Debug enables Info-level logging for the current statement chain: