Mastering Enums in GORM: A Step-by-Step Guide for Go Developers
This tutorial explains how to add enum types to GORM models in Go, covering definition of enum values, model integration, CRUD operations using enums, and alternative approaches such as string fields, custom types, and GORM's built‑in enum tag to enhance type safety and database reliability.
GORM Enum Introduction
GORM is a popular Go ORM that maps structs to database tables and provides CRUD interfaces. Enums allow a variable to take a fixed set of values. This article shows how to add enums to GORM models.
Step 1: Define Enum
type Status string
const (
Pending Status = "pending"
Approved Status = "approved"
Rejected Status = "rejected"
)The Status type has three possible values: "pending", "approved", and "rejected".
Step 2: Define Model
type User struct {
ID uint `gorm:"primary_key"`
Name string
Status Status
CreatedAt time.Time
UpdatedAt time.Time
}The User model includes a field of the enum type Status.
Step 3: Use Enum in GORM Operations
user := User{Name: "John", Status: Pending}
db.Create(&user)
var users []User
db.Where("status = ?", Approved).Find(&users)
db.Model(&user).Update("status", Rejected)
db.Where("status = ?", Pending).Delete(User{})Enum values are used in create, query, update, and delete operations.
Alternative Methods
Method 1: Use String Type Directly
type User struct {
ID uint
Role string
}Using a plain string field can represent roles such as "admin", "moderator", or "user", but it lacks compile‑time checks.
Method 2: Custom Type
type Role string
const (
Admin Role = "admin"
Moderator Role = "moderator"
User Role = "user"
)
type User struct {
ID uint
Role Role
}Defining a custom type with constant values provides compile‑time safety and restricts values to the defined set.
Method 3: GORM Enum Tag
type Role string
const (
Admin Role = "admin"
Moderator Role = "moderator"
User Role = "user"
)
type User struct {
ID uint
Role Role `gorm:"type:enum('admin','moderator','user')"`
}Combining a custom type with GORM's enum column tag creates an actual ENUM column in the database while keeping type safety in Go.
Conclusion
Adding enums to GORM models improves type safety and makes database operations more reliable.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
MaGe Linux Operations
Founded in 2009, MaGe Education is a top Chinese high‑end IT training brand. Its graduates earn 12K+ RMB salaries, and the school has trained tens of thousands of students. It offers high‑pay courses in Linux cloud operations, Python full‑stack, automation, data analysis, AI, and Go high‑concurrency architecture. Thanks to quality courses and a solid reputation, it has talent partnerships with numerous internet firms.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
