GORM 提供的配置可以在初始化时使用

  1. 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
    }

跳过默认事务

为了确保数据一致性,GORM 会在事务里执行写入操作(创建、更新、删除)。如果没有这方面的要求,您可以在初始化时禁用它。

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

命名策略

GORM 允许用户通过覆盖默认的命名策略更改默认的命名约定,这需要实现接口 Namer

  1. type Namer interface {
    TableName(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
    }

默认 NamingStrategy 也提供了几个选项,如:

  1. db, err := gorm.Open(sqlite.Open("gorm.db"), &gorm.Config{
    NamingStrategy: schema.NamingStrategy{
    TablePrefix: "t_", // 表名前缀,`User` 的表名应该是 `t_users`
    SingularTable: true, // 使用单数表名,启用该选项,此时,`User` 的表名应该是 `t_user`
    },
    })

Logger

允许通过覆盖此选项更改 GORM 的默认 logger,参考 Logger 获取详情

NowFunc

更改创建时间使用的函数

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

DryRun

生成 SQL 但不执行,可以用于准备或测试生成的 SQL,参考 会话 获取详情

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

PrepareStmt

PreparedStmt 在执行任何 SQL 时都会创建一个 prepared statement 并将其缓存,以提高后续的效率,参考 会话 获取详情

  1. 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

  1. 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

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