GORM maps Go structs to SQL databases with migrations, associations, hooks, and a fluent query API. It targets developer-friendly access patterns for common CRUD and relationship modeling.
Overview
GORM is an object-relational mapper for Go that connects application structs to relational databases. It focuses on expressive associations, lifecycle hooks, and batch operations while staying approachable for everyday data access.
Relationship modeling covers has-one, has-many, belongs-to, many-to-many, polymorphism, and single-table inheritance patterns. Hooks run before and after create, save, update, delete, and find events. Loading strategies include Preload and Joins for eager fetching.
Transactions support nesting, savepoints, and rollback to a saved point. The API exposes context-aware queries, prepared statement mode, dry-run mode, batch insert, FindInBatches, and map-based results. A SQL builder handles upserts, locking, optimizer and index hints, named arguments, and expression-based create, update, and search.
Schema tools include composite primary keys, indexes, constraints, and auto migrations. Logging is built in, and a plugin API extends behavior—for example database resolvers for multiple databases or read-write splitting and Prometheus metrics. Each documented feature is backed by tests in the project.
Key features
- Struct-to-table mapping and auto migrations
- Rich association types including polymorphism
- Before and after lifecycle hooks
- Transactions with savepoints
- Preload and Joins eager loading
- SQL builder with upsert and locking hints
- Plugin API for resolvers and metrics
- Context, dry-run, and batch operations
Use cases
- Go web services needing SQL persistence
- Applications with complex relational models
- Projects requiring migration automation
- Read-write splitting via resolver plugins
- CRUD APIs backed by relational databases