GORM provides Config can be used during initialization
type Config struct { |
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{ |
NamingStrategy
GORM allows users to change the naming conventions by overriding the default NamingStrategy
which need to implements interface Namer
type Namer interface { |
The default NamingStrategy
also provides few options, like:
db, err := gorm.Open(sqlite.Open("gorm.db"), &gorm.Config{ |
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{ |
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{ |
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{ |
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{ |
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{ |