V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
V2EX 提问指南
thomaswang
V2EX  ›  问与答

gorm 的使用

  •  
  •   thomaswang · 2019-02-20 13:21:50 +08:00 · 1007 次点击
    这是一个创建于 1885 天前的主题,其中的信息可能已经有所发展或是发生改变。
    ![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,这里如何写呢
    2 条回复    2019-02-20 13:42:44 +08:00
    pubby
        1
    pubby  
       2019-02-20 13:34:39 +08:00   ❤️ 1
    gorm:"primary_key;column:IDs"`
    thomaswang
        2
    thomaswang  
    OP
       2019-02-20 13:42:44 +08:00
    @pubby 多谢
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   1482 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 26ms · UTC 17:11 · PVG 01:11 · LAX 10:11 · JFK 13:11
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.