GORM provides Config can be used during initialization

  1. type Config struct {
  2. SkipDefaultTransaction bool
  3. NamingStrategy schema.Namer
  4. Logger logger.Interface
  5. NowFunc func() time.Time
  6. DryRun bool
  7. PrepareStmt bool
  8. DisableNestedTransaction bool
  9. AllowGlobalUpdate bool
  10. DisableAutomaticPing bool
  11. DisableForeignKeyConstraintWhenMigrating bool
  12. }

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

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

NamingStrategy

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

  1. type Namer interface {
  2. TableName(table string) string
  3. SchemaName(table string) string
  4. ColumnName(table, column string) string
  5. JoinTableName(table string) string
  6. RelationshipFKName(Relationship) string
  7. CheckerName(table, column string) string
  8. IndexName(table, column string) string
  9. }

The default NamingStrategy also provides few options, like:

  1. db, err := gorm.Open(sqlite.Open("gorm.db"), &gorm.Config{
  2. NamingStrategy: schema.NamingStrategy{
  3. TablePrefix: "t_", // table name prefix, table for `User` would be `t_users`
  4. SingularTable: true, // use singular table name, table for `User` would be `user` with this option enabled
  5. NoLowerCase: true, // skip the snake_casing of names
  6. NameReplacer: strings.NewReplacer("CID", "Cid"), // use name replacer to change struct/field name before convert it to db name
  7. },
  8. })

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

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

DryRun

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

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

PrepareStmt

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

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

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{
  2. DisableAutomaticPing: true,
  3. })

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{
  2. DisableForeignKeyConstraintWhenMigrating: true,
  3. })