gorm 的使用

2019-02-20 13:21:50 +08:00
 thomaswang
![3CFBE17B-5142-4B07-A12C-94F1DF9B0596.png]( https://i.loli.net/2019/02/20/5c6ce3a73460d.png)

```
// 重设列名
type Animal struct {
AnimalId int64 `gorm:"column:beast_id"` // 设置列名为`beast_id`
Birthday time.Time `gorm:"column:day_of_the_beast"` // 设置列名为`day_of_the_beast`
Age int64 `gorm:"column:age_of_the_beast"` // 设置列名为`age_of_the_beast`
}
```

``` golang
// 使用 tag`primary_key`用来设置主键
type Animal struct {
AnimalId int64 `gorm:"primary_key"` // 设置 AnimalId 为主键
Name string
Age int64
}
```


比如我想把 IDs 作为 pk,这里如何写呢
1010 次点击
所在节点    问与答
2 条回复
pubby
2019-02-20 13:34:39 +08:00
gorm:"primary_key;column:IDs"`
thomaswang
2019-02-20 13:42:44 +08:00
@pubby 多谢

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/536859

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX