在 GORM 中,标签(Tags) 是用于描述 Go 结构体字段的元数据,帮助 GORM 确定如何将字段与数据库中的列进行映射。通过 GORM 的标签机制,你可以控制字段的数据库行为,例如设置主键、外键、索引、唯一性约束、默认值等。下面详细讲解 GORM 中常用的标签。
1. 基本标签说明
GORM 通过结构体标签来描述数据库表字段的特性。标签格式如下:
type Model struct {
FieldName FieldType `gorm:"tag1;tag2;tag3"`
}
gorm
标签的值由多个选项组成,选项之间用分号 ;
分隔。
2. 常用的 GORM 标签
2.1 主键(Primary Key)
用 primaryKey
标签标记一个字段为主键。
type User struct {
ID uint `gorm:"primaryKey"` // 设置 ID 为主键
Name string
}
gorm:"primaryKey"
会将该字段标记为主键,且 GORM 会自动为该字段创建索引。
2.2 外键(Foreign Key)
用 foreignKey
标签定义外键关系。
type Profile struct {
ID uint
UserID uint `gorm:"foreignKey:UserID"` // 外键关联 User 表的 UserID 字段
Bio string
}
type User struct {
ID uint
Name string
Profile Profile
}
在这个例子中,Profile
结构体通过 foreignKey
标签与 User
结构体关联,表明 Profile
中的 UserID
是外键,指向 User
表的 ID
。
2.3 唯一约束(Unique)
用 unique
标签设置字段唯一约束。
type User struct {
ID uint `gorm:"primaryKey"`
Name string `gorm:"unique"` // 设置 Name 字段唯一
}
gorm:"unique"
会创建一个唯一约束,确保该字段值在表中唯一。
2.4 默认值(Default)
用 default
标签设置字段的默认值。
type User struct {
ID uint `gorm:"primaryKey"`
Name string
Age int `gorm:"default:18"` // 设置 Age 字段的默认值为 18
CreatedAt time.Time
}
gorm:"default:18"
设置 Age
字段的默认值为 18
。
2.5 非空(Not Null)
用 not null
标签指定字段不允许为空。
type User struct {
ID uint `gorm:"primaryKey"`
Name string `gorm:"not null"` // 设置 Name 字段不能为空
Age int
}
gorm:"not null"
表示该字段在数据库中不能为 NULL
。
2.6 自增(Auto Increment)
用 autoIncrement
标签设置字段自增。
type User struct {
ID uint `gorm:"primaryKey;autoIncrement"` // 设置 ID 为自增字段
Name string
}
gorm:"autoIncrement"
表示该字段会自动递增,通常与主键一起使用。
2.7 索引(Index)
用 index
标签为字段创建索引。
type User struct {
ID uint `gorm:"primaryKey"`
Name string `gorm:"index"` // 为 Name 字段创建索引
}
gorm:"index"
会为字段创建普通索引,用于加速查询。
2.8 唯一索引(Unique Index)
uniqueIndex
标签为字段创建唯一索引。
type User struct {
ID uint `gorm:"primaryKey"`
Name string `gorm:"uniqueIndex"` // 为 Name 字段创建唯一索引
}
gorm:"uniqueIndex"
在数据库中创建一个唯一索引,保证字段值的唯一性。
2.9 联合索引(Composite Index)
如果想创建联合索引,可以使用 index
标签,并指定多个字段。
type User struct {
ID uint `gorm:"primaryKey"`
Name string `gorm:"index:idx_name_age"` // 定义联合索引
Age int `gorm:"index:idx_name_age"` // 联合索引
}
上面的例子中,gorm:"index:idx_name_age"
为 Name
和 Age
字段创建了一个联合索引,索引名为 idx_name_age
。
2.10 自定义列名(Column)
通过 column
标签自定义数据库中列的名称。
type User struct {
ID uint `gorm:"primaryKey"`
Name string `gorm:"column:username"` // 将结构体字段 Name 映射为数据库列名 username
}
gorm:"column:username"
将 Name
字段映射到数据库表中的 username
列。
2.11 软删除(Soft Delete)
软删除是通过 GORM 提供的 DeletedAt
字段实现的。
type User struct {
ID uint `gorm:"primaryKey"`
Name string
DeletedAt gorm.DeletedAt `gorm:"index"` // 软删除字段
}
gorm.DeletedAt
类型的字段用于标记删除时间,GORM 会自动处理软删除功能,删除记录时不会真正删除数据,而是将 DeletedAt
字段设置为当前时间。
2.12 外键约束(ForeignKey)
如果想指定外键的字段名称,可以使用 foreignKey
和 references
标签。
type User struct {
ID uint `gorm:"primaryKey"`
Name string
}
type Order struct {
ID uint `gorm:"primaryKey"`
UserID uint
User User `gorm:"foreignKey:UserID;references:ID"` // 外键约束
}
在这个例子中,Order
表的 UserID
字段是外键,关联到 User
表的 ID
字段。
3. 标签总结
标签 | 作用 |
---|---|
primaryKey | 设置字段为主键 |
foreignKey | 设置外键关联 |
unique | 设置字段唯一约束 |
default | 设置字段默认值 |
not null | 设置字段不能为空 |
autoIncrement | 设置字段为自增字段 |
index | 为字段创建索引 |
uniqueIndex | 为字段创建唯一索引 |
column | 设置数据库中的列名 |
DeletedAt | 软删除字段,用于标记删除时间 |
foreignKey | 外键约束,指定外键字段和参考字段 |
4. 使用标签的注意事项
- 标签顺序无关紧要,可以按自己的习惯书写,只要标签的内容正确。
- GORM 支持的标签并不限于上述列举的几种,标签的更多使用场景可以参考 GORM 的官方文档。
- 自定义标签:可以根据项目需求自定义标签,例如用于字段验证等,但这些标签通常是程序员自己处理的,不会直接影响 GORM 的行为。
总结
GORM 提供了丰富的标签功能,可以帮助开发者灵活地控制数据库的行为,从而避免编写复杂的 SQL 语句。在日常开发中,熟练掌握这些标签的使用,不仅可以提高开发效率,还可以确保数据库结构的规范性。