我们创建数据表时一般会增加一个deleted_at的字段,主要用来标识这条记录是否删除,及删除的时间。
下面建一张表的结构体
type SysUser struct {
Id int64 `gorm:"column:id;rimary_key"`
LoginName string `gorm:"column:login_name;index;unique;size:32"`
Password string `gorm:"column:password;null"`
LastLogin *time.Time `gorm:"column:last_login;null"`
Locked bool `gorm:"column:locked;null"`
CreatedAt time.Time `gorm:"column:created_at"`
UpdatedAt time.Time `gorm:"column:updated_at"`
DeletedAt *time.Time `gorm:"column:deleted_at;null"`
}
创建一张系统用户表,DeletedAt字段可以为空,类型是*time.Time可以是nil。
然后生成表:
var db *gorm.DB
// 连接数据库 省略
// 创建数据库表
db.CreateTable(&SysUser{}).Er