thomaswang
V2EX  ›  问与答

gorm 的使用

  •  
  •   thomaswang · Feb 20, 2019 · 1511 views
    This topic created in 2645 days ago, the information mentioned may be changed or developed.
    ![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 replies    2019-02-20 13:42:44 +08:00
    pubby
        1
    pubby  
       Feb 20, 2019   ❤️ 1
    gorm:"primary_key;column:IDs"`
    thomaswang
        2
    thomaswang  
    OP
       Feb 20, 2019
    @pubby 多谢
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   1571 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 35ms · UTC 16:42 · PVG 00:42 · LAX 09:42 · JFK 12:42
    ♥ Do have faith in what you're doing.