The Dialector interface
| Method | Responsibility |
|---|---|
Name() string | Returns the dialector’s identifier (e.g., "mysql", "postgres"). |
Initialize(*DB) error | Called once by gorm.Open. Sets up the connection pool, registers callbacks, and configures clause builders. |
Migrator(*DB) Migrator | Returns the implementation of the Migrator interface used by db.AutoMigrate and db.Migrator(). |
DataTypeOf(*schema.Field) string | Maps a GORM schema field to the database-specific SQL type string (e.g., "VARCHAR(255)", "BIGINT"). |
DefaultValueOf(*schema.Field) clause.Expression | Returns the SQL expression for a column’s default value. |
BindVarTo(writer, stmt, v) | Writes the bind variable placeholder for a query parameter (e.g., ? for MySQL, $1 for PostgreSQL). |
QuoteTo(writer, string) | Writes a quoted identifier (e.g., `name` for MySQL, "name" for PostgreSQL). |
Explain(sql, vars) string | Interpolates bind variables into an SQL string for logging purposes only — never for execution. |
When to implement a custom dialector
Adding support for a new database
Adding support for a new database
If you want to use GORM with a database that doesn’t have an official driver, implement the full
Dialector interface along with a Migrator. You’ll need to handle SQL generation, quoting, data type mapping, and connection pool initialization.Wrapping an existing dialector
Wrapping an existing dialector
If you need to intercept or modify SQL before it reaches the database — for example, to add query hints, enforce row limits, or log to an external system — embed an existing dialector and override only the methods you need.
The ConnPool interface
GORM usesConnPool as the abstraction over the underlying database connection. The dialector is responsible for providing a ConnPool-compatible value during Initialize.
*sql.DB and *sql.Tx types from the database/sql package both satisfy this interface, so most dialectors assign db.ConnPool = sqlDB inside Initialize.
You can also provide a custom ConnPool to intercept all query execution — for example, to add request tracing or enforce timeouts:
Additional dialector interfaces
GORM checks for these optional interfaces at runtime and activates additional behavior when they are present.SavePointerDialectorInterface
Implement this interface to enable savepoint support inside nested transactions:db.Transaction() it will call SavePoint to create a savepoint and RollbackTo to roll back to it on error, rather than issuing a full ROLLBACK.
ErrorTranslator
Implement this interface to map database-specific errors to GORM’s standard sentinel errors (such asgorm.ErrDuplicatedKey or gorm.ErrForeignKeyViolated):
Translate on every error before returning it to the caller when TranslateError: true is set in gorm.Config:
If
TranslateError: true is set in gorm.Config but the dialector does not implement ErrorTranslator, GORM will log a warning and return raw database errors.Implementing a minimal dialector
The following skeleton shows the minimum required to satisfy theDialector interface. Fill in each method with the SQL syntax of your target database:
Related interfaces
The following interfaces fromgorm.io/gorm may also be relevant when building a dialector:
| Interface | Purpose |
|---|---|
TxBeginner | Implemented by *sql.DB. Allows GORM to start transactions with BeginTx. |
ConnPoolBeginner | Alternative to TxBeginner for connection pools that return a ConnPool rather than a *sql.Tx. |
TxCommitter | Implemented by *sql.Tx. Used by GORM to commit or roll back transactions. |
GetDBConnector | Implement on your ConnPool to let db.DB() return the underlying *sql.DB. |
Valuer | Implement on model field types to provide a custom clause.Expr for SQL generation. |