Gorm: 声明模型(1)
Gorm: 声明模型(1)
建议点击 查看原文 查看最新内容。
原文链接: https://typonotes.com/posts/2025/03/17/gorm-model-declaration/
1. 数组字段
https://gorm.io/zh_CN/docs/models.html
如果表中有 slice 字段, 则需要使用 type
指定类型。
|
|
2. 索引
https://gorm.io/zh_CN/docs/indexes.html
2.1 唯一作引
唯一索引有两种形式,
uniqueIndex
index:[name],unique
: 自定义索引名字
|
|
2.2. 复合索引
多个字段使用相同的索引名字, 则创建 复合索引
|
|
3. 外键
https://gorm.io/zh_CN/docs/constraints.html
通过 constraint
标签的 OnDelete
, OnUpdate
选项设置外键约束,
|
|
3.1. 禁用外键约束
在初始化数据库的时候, 使用参数 DisableForeignKeyConstraintWhenMigrating
禁用外键约束。
|
|
- 原文链接:https://typonotes.com/posts/2025/03/17/gorm-model-declaration/
- 本文为原创文章,转载注明出处。
- 欢迎 扫码关注公众号
Go与云原生
或 订阅网站 https://typonotes.com/ 。 - 第一时间看后续精彩文章。觉得好的话,请猛击文章右下角「在看」,感谢支持。
