GORM ORM In a Nutshell
Why GORM?
Because raw SQL is like writing assembly when you don’t need to. GORM makes database operations much easier while still letting you drop down to raw SQL when you need to.
GORM supports multiple databases, including PostgreSQL, MySQL, SQLite, and SQL Server. It comes with great features like:
- Auto Migrations – No more writing boring migration scripts.
- Relations – Handle associations easily.
- Hooks – Execute logic before/after actions.
- Transactions – Wrap operations in transactions effortlessly.
- Eager Loading – Fetch related records with ease.
- Raw SQL Support – Sometimes you just need that custom SQL.
Let’s dive in and see some code!
Getting Started
Installation
First, install GORM and the appropriate database driver:
1
2
3
4
5
| go get -u gorm.io/gorm
go get -u gorm.io/driver/sqlite # SQLite
go get -u gorm.io/driver/mysql # MySQL
go get -u gorm.io/driver/postgres # PostgreSQL
|
Connecting to a Database
Here’s how you connect to different databases:
SQLite Example
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
| package main
import (
"gorm.io/driver/sqlite"
"gorm.io/gorm"
"log"
)
func main() {
db, err := gorm.Open(sqlite.Open("test.db"), &gorm.Config{})
if err != nil {
log.Fatal("Failed to connect to database")
}
log.Println("Connected to SQLite!")
}
|
MySQL Example
1
2
3
4
| import "gorm.io/driver/mysql"
dsn := "user:password@tcp(127.0.0.1:3306)/dbname?charset=utf8mb4&parseTime=True&loc=Local"
db, err := gorm.Open(mysql.Open(dsn), &gorm.Config{})
|
PostgreSQL Example
1
2
3
4
| import "gorm.io/driver/postgres"
dsn := "host=localhost user=youruser password=yourpassword dbname=yourdb port=5432 sslmode=disable"
db, err := gorm.Open(postgres.Open(dsn), &gorm.Config{})
|
Defining Models
Now, let’s define a model and let GORM do the heavy lifting.
1
2
3
4
5
| type User struct {
ID uint `gorm:"primaryKey"`
Name string `gorm:"size:255"`
Email string `gorm:"unique"`
}
|
Auto Migration
You don’t need to write CREATE TABLE
statements manually. GORM can do it for you.
1
| db.AutoMigrate(&User{})
|
CRUD Operations
Creating a Record
1
2
| user := User{Name: "John Doe", Email: "john@example.com"}
db.Create(&user)
|
Retrieving Records
1
2
3
| var user User
db.First(&user, 1) // Get user with ID 1
db.First(&user, "email = ?", "john@example.com")
|
Updating a Record
1
| db.Model(&user).Update("Name", "Johnny")
|
Deleting a Record
Relationships
One-to-Many
1
2
3
4
5
6
7
| type Order struct {
ID uint
UserID uint
User User
}
db.AutoMigrate(&Order{})
|
Many-to-Many
1
2
3
4
5
6
7
| type Product struct {
ID uint
Name string
Users []User `gorm:"many2many:user_products"`
}
db.AutoMigrate(&Product{})
|
Raw SQL
If GORM gets in your way, you can always write raw SQL:
1
| rows, err := db.Raw("SELECT * FROM users WHERE name = ?", "John").Rows()
|
Comparing GORM with Other ORMs
Feature | GORM | Ent (Go) | SQLBoiler | xORM (Python) |
---|
Auto Migration | ✅ | ✅ | ❌ | ✅ |
Relations | ✅ | ✅ | ✅ | ✅ |
Hooks | ✅ | ✅ | ✅ | ✅ |
Raw SQL | ✅ | ✅ | ✅ | ✅ |
Transactions | ✅ | ✅ | ✅ | ✅ |
Ease of Use | ✅ | ❌ | ❌ | ✅ |
Key Ideas
Concept | Summary |
---|
GORM Overview | GORM is a powerful ORM for Golang, simplifying database interactions. |
Features | Supports auto migrations, relationships, hooks, transactions, and raw SQL. |
Supported Databases | Works with PostgreSQL, MySQL, SQLite, and SQL Server. |
CRUD Operations | Easily create, read, update, and delete records. |
Relationships | Supports one-to-many and many-to-many relationships. |
Comparisons | GORM stacks up well against other ORMs with strong feature support. |
References