GORM模型定义速查
type User struct { gorm.Model // 这里边包含了主键子增id,创建时间,修改时间,删除时间 Name string `gorm:"<-:create"` // 只能读和创建 Name string `gorm:"<-:update"` // 只能读和修改 Name string `gorm:"<-"` // 读,创建,修改 Name string `gorm:"<-:false"` // 读, 关闭写权限 Name string `gorm:"->"` // 只读 Name string `gorm:"->;<-:create"` // 读和创建 Name string `gorm:"->:false;<-:create"` // 只能新增 Name string `gorm:"-"` // 创建和读的时候从struct忽略这个字段 Name string `gorm:"-:all"` // 在数据迁移,读和新增的时候都忽略 ignore this field when write, read and migrate with struct Name string `gorm:"-:migration"` // 迁移的时候忽略这个字段 ignore this field when migrate with struct CreatedAt time.Time // 在创建时,如果该字段值为零值,则使用当前时间填充 UpdatedAt int // 在创建时该字段值为零值或者在更新时,使用当前时间戳秒数填充 Updated int64 `gorm:"autoUpdateTime:nano"` // 使用时间戳填纳秒数充更新时间 Updated int64 `gorm:"autoUpdateTime:milli"` // 使用时间戳毫秒数填充更新时间 Created int64 `gorm:"autoCreateTime"` // 使用时间戳秒数填充创建时间 Name string `gorm:"varchar(200) not null"` // 指定字段数据结构 bool、int、uint、float、string、time、bytes Name string `gorm:"size(200)"` // 指定列大小 Name string `gorm:"primaryKey"` // 设置为主键 Name string `gorm:"unique"` // 唯一 Name string `gorm:"default(123)"` // 设置默认值 Name string `gorm:"precision(2)"` // 精度 ID int `gorm:"autoIncrement"` // 自增长 } // gorm.Model 的定义 type Model struct { ID uint `gorm:"primaryKey"` CreatedAt time.Time UpdatedAt time.Time DeletedAt gorm.DeletedAt `gorm:"index"` }
本文来自博客园,作者:颖小主,转载请注明原文链接:https://www.cnblogs.com/yingxiaozhu/p/16160648.html
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· winform 绘制太阳,地球,月球 运作规律
· AI与.NET技术实操系列(五):向量存储与相似性搜索在 .NET 中的实现
· 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人
· 上周热点回顾(3.3-3.9)
· AI 智能体引爆开源社区「GitHub 热点速览」