GORM
Overview
GORM is the de-facto ORM library for Go, aiming to be developer-friendly. It maps Go structs to tables and provides associations, hooks, transactions, eager loading, and auto-migration, with driver support for PostgreSQL, MySQL, SQLite, SQL Server, and others. Read this when building Go services that need a full-featured ORM rather than hand-written SQL.
Official sources
Install / setup
go get -u gorm.io/gorm
go get -u gorm.io/driver/sqlite
Core concepts
- Models: plain Go structs whose fields map to columns; embed
gorm.Model for ID/timestamps/soft-delete.
*gorm.DB: the database handle opened with gorm.Open(driver, &gorm.Config{}); carries chainable method calls.
- CRUD chain methods:
Create, First/Find, Where, Update, Delete build and run queries.
- Associations:
Has One, Has Many, Belongs To, Many To Many with Preload for eager loading.
- AutoMigrate: creates/updates tables to match struct definitions.
- Hooks & transactions: lifecycle callbacks (
BeforeCreate etc.) and db.Transaction(func...) for atomic work.
Best practices
- Open the database once and reuse the shared
*gorm.DB; it manages an underlying connection pool.
- Use
Preload (or joins) to load associations and avoid N+1 queries (per docs).
- Treat
AutoMigrate as convenient for development; review schema changes carefully before relying on it in production (it only adds, it will not delete columns).
- Always check the returned
error (and RowsAffected) on query chains.
Common pitfalls
- Ignoring the
.Error field on a chained call → silent failures; inspect result.Error after each operation.
- Expecting
AutoMigrate to drop or rename columns → it does not remove columns; use explicit migrations for destructive changes.
Examples
import (
"gorm.io/gorm"
"gorm.io/driver/sqlite"
)
type User struct {
gorm.Model
Name string
}
db, _ := gorm.Open(sqlite.Open("app.db"), &gorm.Config{})
db.AutoMigrate(&User{})
db.Create(&User{Name: "Ada"})
var u User
db.Where("name = ?", "Ada").First(&u)
Further reading
Related skills
- ../diesel — Rust's type-safe ORM (contrast: another typed-language ORM)
- ../prisma — type-safe ORM in the TS ecosystem
1---2name: gorm3description: The fantastic, developer-friendly ORM for Go with associations, hooks, transactions, and auto-migration; consult for relational data access in Go.4domain: stack5category: orm6tags: [orm, go, golang, database, migrations, associations]7official_sources:8 - https://gorm.io/docs/9 - https://github.com/go-gorm/gorm10verified: 2026-06-1611metadata:12 source: https://skillmd.com/skills/gabriel7silva/gorm13---1415# GORM1617## Overview18GORM is the de-facto ORM library for Go, aiming to be developer-friendly. It maps Go structs to tables and provides associations, hooks, transactions, eager loading, and auto-migration, with driver support for PostgreSQL, MySQL, SQLite, SQL Server, and others. Read this when building Go services that need a full-featured ORM rather than hand-written SQL.1920## Official sources21- Docs: https://gorm.io/docs/22- Repo: https://github.com/go-gorm/gorm23- Install / index: https://gorm.io/docs/index.html2425## Install / setup26```bash27go get -u gorm.io/gorm28go get -u gorm.io/driver/sqlite29```3031## Core concepts32- **Models**: plain Go structs whose fields map to columns; embed `gorm.Model` for ID/timestamps/soft-delete.33- **`*gorm.DB`**: the database handle opened with `gorm.Open(driver, &gorm.Config{})`; carries chainable method calls.34- **CRUD chain methods**: `Create`, `First`/`Find`, `Where`, `Update`, `Delete` build and run queries.35- **Associations**: `Has One`, `Has Many`, `Belongs To`, `Many To Many` with `Preload` for eager loading.36- **AutoMigrate**: creates/updates tables to match struct definitions.37- **Hooks & transactions**: lifecycle callbacks (`BeforeCreate` etc.) and `db.Transaction(func...)` for atomic work.3839## Best practices40- Open the database once and reuse the shared `*gorm.DB`; it manages an underlying connection pool.41- Use `Preload` (or joins) to load associations and avoid N+1 queries (per docs).42- Treat `AutoMigrate` as convenient for development; review schema changes carefully before relying on it in production (it only adds, it will not delete columns).43- Always check the returned `error` (and `RowsAffected`) on query chains.4445## Common pitfalls46- Ignoring the `.Error` field on a chained call → silent failures; inspect `result.Error` after each operation.47- Expecting `AutoMigrate` to drop or rename columns → it does not remove columns; use explicit migrations for destructive changes.4849## Examples50```go51import (52 "gorm.io/gorm"53 "gorm.io/driver/sqlite"54)5556type User struct {57 gorm.Model58 Name string59}6061db, _ := gorm.Open(sqlite.Open("app.db"), &gorm.Config{})62db.AutoMigrate(&User{})63db.Create(&User{Name: "Ada"})6465var u User66db.Where("name = ?", "Ada").First(&u)67```6869## Further reading70- https://gorm.io/docs/models.html — declaring models71- https://gorm.io/docs/preload.html — preloading associations7273## Related skills74- ../diesel — Rust's type-safe ORM (contrast: another typed-language ORM)75- ../prisma — type-safe ORM in the TS ecosystem