The fantastic ORM library for Golang
GORM is a full-featured ORM library for Go that makes working with databases easy, safe, and productive. It provides a clean, chainable API that handles everything from simple CRUD operations to complex associations, transactions, and migrations.Quickstart
Get up and running with GORM in minutes with a working example
Connecting to Database
Connect to MySQL, PostgreSQL, SQLite, SQL Server, and more
CRUD Operations
Create, read, update, and delete records with a clean chainable API
API Reference
Full reference for the DB type, chainable methods, and finisher methods
Why GORM?
GORM is the most popular ORM library in the Go ecosystem with over 39,000 GitHub stars. It provides everything you need to work with relational databases in Go without sacrificing developer experience.Full-Featured ORM
Associations, hooks, transactions, migrations, and more out of the box
Developer Friendly
Chainable API, clear error handling, and comprehensive documentation
Extensible
Plugin system for database resolvers, Prometheus metrics, and more
Multi-Database
Supports MySQL, PostgreSQL, SQLite, SQL Server, and custom dialectors
Safe by Default
Transactions by default, soft deletes, and protection against global updates
Type-Safe
Generic API with
G[T] for compile-time type safety in Go 1.18+Key Features
Associations
Associations
GORM supports all relationship types: Has One, Has Many, Belongs To, Many To Many, as well as Polymorphism and Single-table inheritance. Associations are defined directly in your Go structs using struct tags.
Hooks (Lifecycle Callbacks)
Hooks (Lifecycle Callbacks)
Define Before/After hooks for Create, Save, Update, Delete, and Find operations. Hooks are methods on your model structs and integrate seamlessly with GORM’s callback system.
Eager Loading
Eager Loading
Load associated records efficiently using
Preload for separate queries or Joins for SQL JOINs. Both support conditional loading with additional where clauses.Transactions
Transactions
GORM wraps single create/update/delete operations in transactions by default. You can use
Transaction() for multi-step operations, and GORM supports nested transactions and savepoints.Auto Migration
Auto Migration
Automatically migrate your schema using
AutoMigrate(). GORM will create tables, missing columns, and missing indexes while preserving existing data.SQL Builder
SQL Builder
Drop down to raw SQL when needed with
Raw() and Exec(), or use the clause package to build SQL expressions programmatically. Supports named arguments, upsert, locking, and optimizer hints.Installation
Quick Example
Supported Databases
| Database | Driver Package |
|---|---|
| MySQL | gorm.io/driver/mysql |
| PostgreSQL | gorm.io/driver/postgres |
| SQLite | gorm.io/driver/sqlite |
| SQL Server | gorm.io/driver/sqlserver |
| ClickHouse | gorm.io/driver/clickhouse |