Config struct is passed to gorm.Open to control GORM behaviour at the session level. It implements the Option interface, so you pass it directly as the second argument to Open.
Transaction behaviour
When
true, GORM does not wrap single create, update, and delete operations in an implicit transaction. Improves performance for workloads that do not need the extra safety.If greater than zero, a context timeout is automatically applied to each transaction started with
Begin (unless the context already has a deadline).A default timeout duration applied to statement contexts when no explicit deadline is set.
When
true, calling Transaction inside an existing transaction does not create a savepoint. The inner fc executes directly on the outer transaction.When
false (the default), GORM returns ErrMissingWhereClause if you attempt an update or delete without any WHERE conditions. Set to true to allow global updates.Naming strategy
Controls how Go struct and field names are converted to table and column names. The default implementation (
schema.NamingStrategy) snake-cases names and pluralises table names. Common fields:Associations
When
true, GORM performs a full upsert of all associations when saving a record (instead of only updating foreign keys). Useful when you want a single Save call to persist nested structs.When
true, the Unscoped flag is propagated to every nested statement, including preloads and association queries.Logging
The logger implementation. Defaults to
logger.Default, which writes to stdout. Use logger.Default.LogMode(logger.Silent) to suppress all output, or provide a custom implementation.When
true, database-specific errors returned by the dialector are translated to standard GORM errors such as ErrDuplicatedKey, ErrForeignKeyViolated, and ErrCheckConstraintViolated. The dialector must implement ErrorTranslator.Time
The function GORM calls to obtain the current time when setting
CreatedAt, UpdatedAt, or DeletedAt fields. Defaults to time.Now().Local(). Override to use UTC or a custom clock.Statement preparation
When
true, GORM generates SQL but does not execute it. The generated SQL is available on db.Statement.SQL. Useful for inspection and testing.When
true, GORM caches prepared statements. Subsequent identical queries reuse the cached statement, reducing parsing overhead on the database server.Maximum number of entries in the prepared statement LRU cache. A value of
0 uses the maximum int64 value (effectively unlimited).Time-to-live for entries in the prepared statement LRU cache. Expired entries are evicted on the next access.
Querying
When
true, SELECT statements explicitly list every column name from the model instead of using SELECT *. Useful when tables have columns that conflict with GORM’s scanning logic.Default batch size used by
Create when Config.CreateBatchSize > 0. Individual calls to CreateInBatches can override this per-call.Migrations
When
true, GORM does not call Ping() on the connection pool after Open. Useful when connecting to databases that do not support ping, or when you want to defer the first connection.When
true, AutoMigrate and Migrator do not create foreign key constraints. The relationships are still inferred in Go but not enforced at the database level.When
true, AutoMigrate ignores all relationships and does not create join tables or foreign key constraints.Low-level / advanced
A map of custom clause builders keyed by clause name. Use this to override or extend how SQL clauses are rendered for a specific dialector.
The underlying connection pool. Normally set by the dialector during
Initialize. You can supply a custom pool (e.g., a *sql.DB you configure yourself) before calling Open.The database dialector embedded in
Config. Set automatically by Open from the first argument.The registry of plugins registered via
db.Use(...). Keyed by plugin name.