定义模型

GORM 用 Go 结构体描述数据库表。

一个结构体通常对应一张表,一个字段通常对应表里的一列。

一、最简单的模型

type User struct {
    ID    uint
    Name  string
    Email string
}

默认情况下:

GoMySQL
Userusers
IDid 字段
Namename 字段
Emailemail 字段

GORM 默认把结构体名转成复数表名,把字段名转成下划线字段名。

二、使用 gorm.Model

GORM 内置了一个基础模型:

type Model struct {
    ID        uint `gorm:"primarykey"`
    CreatedAt time.Time
    UpdatedAt time.Time
    DeletedAt gorm.DeletedAt `gorm:"index"`
}

可以直接嵌入:

type User struct {
    gorm.Model
    Name  string
    Email string
}

它会自动包含:

字段含义
ID主键
CreatedAt创建时间
UpdatedAt更新时间
DeletedAt软删除时间

不使用软删除时,可以不嵌入 gorm.Model,自己定义字段。

三、推荐的显式模型

业务项目里更常见的是自己写清楚字段:

type User struct {
    ID        uint      `gorm:"primaryKey"`
    Name      string    `gorm:"size:50;not null"`
    Email     string    `gorm:"size:100;not null;uniqueIndex"`
    Status    string    `gorm:"size:20;not null;default:active"`
    CreatedAt time.Time
    UpdatedAt time.Time
}

这样字段约束更明确,也不会默认带上软删除。

四、常用标签

GORM 使用结构体标签控制字段。

Email string `gorm:"size:100;not null;uniqueIndex"`

常见标签:

标签含义
primaryKey主键
size:100字符串长度
not null非空
unique唯一约束
uniqueIndex唯一索引
index普通索引
default:active默认值
column:user_name指定字段名
type:text指定数据库类型
-忽略字段,不映射到数据库

多个标签用分号隔开。

五、字段命名规则

type Article struct {
    ID        uint
    UserID    uint
    Title     string
    ViewCount int
    CreatedAt time.Time
}

默认字段名:

Go 字段数据库字段
IDid
UserIDuser_id
Titletitle
ViewCountview_count
CreatedAtcreated_at

指定字段名:

UserName string `gorm:"column:user_name"`

六、指定表名

默认情况下,User 对应 users

如果要指定表名,可以实现 TableName 方法:

func (User) TableName() string {
    return "app_users"
}

一般不建议一开始就自定义表名。除非已有数据库表名不符合 GORM 默认约定。

七、时间字段

如果模型里有这些字段:

CreatedAt time.Time
UpdatedAt time.Time

GORM 会自动维护:

  • 创建时填充 CreatedAt
  • 创建和更新时填充 UpdatedAt

示例:

type Article struct {
    ID        uint      `gorm:"primaryKey"`
    Title     string    `gorm:"size:200;not null"`
    Content   string    `gorm:"type:text;not null"`
    CreatedAt time.Time
    UpdatedAt time.Time
}

八、软删除字段

如果模型包含:

DeletedAt gorm.DeletedAt `gorm:"index"`

执行删除时,GORM 默认不会真正删除数据,而是更新 deleted_at

type User struct {
    ID        uint `gorm:"primaryKey"`
    Name      string
    DeletedAt gorm.DeletedAt `gorm:"index"`
}

查询时,GORM 默认会自动过滤已经软删除的数据。

本地练习时要先知道这个行为,避免明明数据库里有数据,GORM 却查不到。

九、完整用户模型

package model

import "time"

type User struct {
    ID        uint      `gorm:"primaryKey"`
    Name      string    `gorm:"size:50;not null"`
    Email     string    `gorm:"size:100;not null;uniqueIndex"`
    Status    string    `gorm:"size:20;not null;default:active"`
    CreatedAt time.Time
    UpdatedAt time.Time
}

这就是后面 CRUD 示例使用的基础模型。