Skip to main content
GORM

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

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.
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.
Load associated records efficiently using Preload for separate queries or Joins for SQL JOINs. Both support conditional loading with additional where clauses.
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.
Automatically migrate your schema using AutoMigrate(). GORM will create tables, missing columns, and missing indexes while preserving existing data.
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

go get -u gorm.io/gorm
You also need a database driver:
go get -u gorm.io/driver/mysql

Quick Example

package main

import (
  "gorm.io/driver/sqlite"
  "gorm.io/gorm"
)

type Product struct {
  gorm.Model
  Code  string
  Price uint
}

func main() {
  db, err := gorm.Open(sqlite.Open("test.db"), &gorm.Config{})
  if err != nil {
    panic("failed to connect database")
  }

  // Migrate the schema
  db.AutoMigrate(&Product{})

  // Create
  db.Create(&Product{Code: "D42", Price: 100})

  // Read
  var product Product
  db.First(&product, 1)                 // find product with integer primary key
  db.First(&product, "code = ?", "D42") // find product with code D42

  // Update - update product's price to 200
  db.Model(&product).Update("Price", 200)
  // Update - update multiple fields
  db.Model(&product).Updates(Product{Price: 200, Code: "F42"}) // non-zero fields
  db.Model(&product).Updates(map[string]interface{}{"Price": 200, "Code": "F42"})

  // Delete - delete product
  db.Delete(&product, 1)
}

Supported Databases

DatabaseDriver Package
MySQLgorm.io/driver/mysql
PostgreSQLgorm.io/driver/postgres
SQLitegorm.io/driver/sqlite
SQL Servergorm.io/driver/sqlserver
ClickHousegorm.io/driver/clickhouse

License

GORM is released under the MIT License. © Jinzhu, 2013~time.Now

Build docs developers (and LLMs) love