API Reference
GORM
Developer-friendly Go ORM with chainable queries, hooks, eager loading, and migrations.
Official Documentation ↗CompatibilityThis documentation covers version
v1.25. Check the official docs for the latest API surface.Installation
go
go get gorm.io/gormMethods
No methods match this filter.
GORM is a developer-friendly ORM for Go. It provides a full-featured, chainable API for CRUD, queries, hooks, eager loading, transactions, and migrations across multiple databases.
Key Features
- Chainable API: Build queries fluently with Where, Order, Limit
- Hooks: Before/After Create, Update, Delete, Find
- Relations: Has one, has many, many to many with Preload
- Soft delete: Built-in soft deletes via DeletedAt
- Migrations: AutoMigrate and schema tools
Errors
The official API reference declares the following error types:
gorm.ErrRecordNotFound(var error (logger.ErrRecordNotFound)) — official text: “ // ErrRecordNotFound record not found error ErrRecordNotFound = logger.ErrRecordNotFound” (source: https://raw.githubusercontent.com/go-gorm/gorm/v1.31.2/errors.go, fetched 2026-11-08T15:51:26+08:00)gorm.ErrDuplicatedKey(var error (errors.New)) — official text: “ // ErrDuplicatedKey occurs when there is a unique key constraint violation ErrDuplicatedKey = errors.New(“duplicated key not allowed”)” (source: https://raw.githubusercontent.com/go-gorm/gorm/v1.31.2/errors.go, fetched 2026-11-08T15:51:26+08:00)gorm.ErrForeignKeyViolated(var error (errors.New)) — official text: “ // ErrForeignKeyViolated occurs when there is a foreign key constraint violation ErrForeignKeyViolated = errors.New(“violates foreign key constraint”)” (source: https://raw.githubusercontent.com/go-gorm/gorm/v1.31.2/errors.go, fetched 2026-11-08T15:51:26+08:00)
Security
Official security statements captured from the package documentation:
- “GORM uses the
database/sql’s argument placeholders to construct the SQL statement, which will automatically escape arguments to avoid SQL injection” (source: https://gorm.io/docs/security.html, fetched 2026-11-08T15:51:26+08:00)
API reference evidence
Fetched from official sources on 2026-11-08T15:51:26+08:00 via WebFetch pkg.go.dev + WebFetch raw.githubusercontent.com (go-gorm/gorm v1.31.2) + WebFetch gorm.io/docs/security.html.
gorm.Open→ signature from https://raw.githubusercontent.com/go-gorm/gorm/v1.31.2/gorm.go; official text: “func Open(dialector Dialector, opts …Option) (db *DB, err error) {”AutoMigrate→ signature from https://raw.githubusercontent.com/go-gorm/gorm/v1.31.2/migrator.go; official text: “func (db *DB) AutoMigrate(dst …interface{}) error { return db.Migrator().AutoMigrate(dst…) }”Create→ signature from https://raw.githubusercontent.com/go-gorm/gorm/v1.31.2/finisher_api.go; official text: “// Create inserts value, returning the inserted data’s primary key in value’s id func (db *DB) Create(value interface{}) (tx *DB) {”Where / Find→ signature from https://raw.githubusercontent.com/go-gorm/gorm/v1.31.2/chainable_api.go; official text: “func (db *DB) Where(query interface{}, args …interface{}) (tx *DB) {”Preload→ signature from https://raw.githubusercontent.com/go-gorm/gorm/v1.31.2/chainable_api.go; official text: “func (db *DB) Preload(query string, args …interface{}) (tx *DB) {”
FAQ
- What is gorm.Model and should I embed it?
- gorm.Model embeds ID, CreatedAt, UpdatedAt, and DeletedAt fields, giving soft delete and timestamps. Embed it for standard models, or define your own primary key and timestamps if you need custom behavior.
- How do I avoid N+1 queries in GORM?
- Use Preload to eager load associations in a single extra query per relation. Without Preload, accessing a relation triggers a separate query per record, causing N+1.
- Is AutoMigrate safe for production?
- AutoMigrate creates missing tables and columns but only adds, never removes, fields. It is acceptable for simple changes but should not replace versioned migrations for production schema control. Use gorm/migrator or a migration tool.
Report a problem with this API docOpens a pre-filled issue on GitHub.