Advanced Topics

GORM Config

qianmoQqianmoQ· 更新于 2026-09-15· 阅读 10 分钟· 0 次阅读

登录后可跨设备保存划线和私人笔记登录

GORM provides Config can be used during initialization

type Config struct {
  SkipDefaultTransaction   bool
  NamingStrategy           schema.Namer
  Logger                   logger.Interface
  NowFunc                  func() time.Time
  DryRun                   bool
  PrepareStmt              bool
  DisableNestedTransaction bool
  AllowGlobalUpdate        bool
  DisableAutomaticPing     bool
  DisableForeignKeyConstraintWhenMigrating bool
}

SkipDefaultTransaction

GORM perform write (create/update/delete) operations run inside a transaction to ensure data consistency, you can disable it during initialization if it is not required

db, err := gorm.Open(sqlite.Open("gorm.db"), &gorm.Config{
  SkipDefaultTransaction: true,
})

NamingStrategy

GORM allows users to change the naming conventions by overriding the default NamingStrategy which need to implements interface Namer

type Namer interface {
  TableName(table string) string
  SchemaName(table string) string
  ColumnName(table, column string) string
  JoinTableName(table string) string
  RelationshipFKName(Relationship) string
  CheckerName(table, column string) string
  IndexName(table, column string) string
}

The default NamingStrategy also provides few options, like:

db, err := gorm.Open(sqlite.Open("gorm.db"), &gorm.Config{
  NamingStrategy: schema.NamingStrategy{
    TablePrefix: "t_",   
    SingularTable: true, 
    NoLowerCase: true, 
    NameReplacer: strings.NewReplacer("CID", "Cid"), 
  },
})

Logger

Allow to change GORM’s default logger by overriding this option, refer Logger for more details

NowFunc

Change the function to be used when creating a new timestamp

db, err := gorm.Open(sqlite.Open("gorm.db"), &gorm.Config{
  NowFunc: func() time.Time {
    return time.Now().Local()
  },
})

DryRun

Generate SQL without executing, can be used to prepare or test generated SQL, refer Session for details

db, err := gorm.Open(sqlite.Open("gorm.db"), &gorm.Config{
  DryRun: false,
})

PrepareStmt

PreparedStmt creates a prepared statement when executing any SQL and caches them to speed up future calls, refer Session for details

db, err := gorm.Open(sqlite.Open("gorm.db"), &gorm.Config{
  PrepareStmt: false,
})

DisableNestedTransaction

When using Transaction method inside a db transaction, GORM will use SavePoint(savedPointName), RollbackTo(savedPointName) to give you the nested transaction support, you could disable it by using the DisableNestedTransaction option, refer Session for details

AllowGlobalUpdate

Enable global update/delete, refer Session for details

DisableAutomaticPing

GORM automatically ping database after initialized to check database availability, disable it by setting it to true

db, err := gorm.Open(sqlite.Open("gorm.db"), &gorm.Config{
  DisableAutomaticPing: true,
})

DisableForeignKeyConstraintWhenMigrating

GORM creates database foreign key constraints automatically when AutoMigrate or CreateTable, disable this by setting it to true, refer Migration for details

db, err := gorm.Open(sqlite.Open("gorm.db"), &gorm.Config{
  DisableForeignKeyConstraintWhenMigrating: true,
})

GitHub tag (latest SemVer)

评论

登录后参与评论

正在加载评论…