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

gogo get gorm.io/gorm

Methods

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:

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.

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.