dongwu8064 2018-11-23 10:00
浏览 1044

如何正确调用gorm别名?

Here is my code:

package main

import (
    "fmt"
    "time"

    "github.com/jinzhu/gorm"
    _ "github.com/jinzhu/gorm/dialects/sqlite"
)

type ClientCustomer struct {
    Id       int `json:"Id"`
    Name     string
    Created  time.Time
    key      string
    UserId   int `gorm:"user_id"`
    Modified time.Time
}

func (ClientCustomer) TableName() string {
    return "Client_customer"
}

type ClientCustomerInvitation struct {
    Id               int
    CustomerId       int `gorm:"customer_id"`
    CodeInvitationId int `gorm:"codeinvitation_id"`
}

func (ClientCustomerInvitation) TableName() string {
    return "Client_customer_invitation"
}

func main() {
    db, err := gorm.Open("sqlite3", "db.sqlite3?cache=shared&mode=rwc")
    if err != nil {
        panic("failed to connect database")
    }
    defer db.Close()
    var clientCustomer ClientCustomer
    rows, err := db.Model(&ClientCustomer{}).Rows()
    defer rows.Close()
    if err != nil {
        panic(err)
    }
    var clientCustomerInvitation ClientCustomerInvitation
    for rows.Next() {
        db.ScanRows(rows, &clientCustomer)
        db.First(&clientCustomerInvitation, "customer_id = ?", clientCustomer.Id)
        fmt.Println(clientCustomer)
        fmt.Println(clientCustomerInvitation)

    }

}

but I'm not fond of this line:

db.First(&clientCustomerInvitation, "customer_id = ?", clientCustomer.Id)

Is there a way to call "customer_id" from the struct directly instead of using a string?

Ideally I would like to do something like:

 db.First(&clientCustomerInvitation, ClientCustomerInvitation.CustomerId.gormAlias+" = ?", clientCustomer.Id)

I'm looking for a way to use the gorm alias for mapping the field in way that is more elegant and re usable than a mere string.

  • 写回答

1条回答 默认 最新

  • douweibeng5219 2018-11-23 10:13
    关注

    The only way to be able to get tag value from certain struct field, is by using reflect.

    My suggestion, create a function that return tag value from specific struct field. Something like below:

    func getGormAlias(obj interface{}, fieldName string) string {
        if field, ok := reflect.TypeOf(obj).FieldByName(fieldName); ok {
            return field.Tag.Get("gorm")
        }
    
        return ""
    }
    

    Then use it to get the tag value.

    gormAliasCustomerId := getGormAlias(ClientCustomerInvitation{}, "CustomerId")
    db.First(&clientCustomerInvitation, gormAliasCustomerId + " = ?", clientCustomer.Id)
    

    Basically what getGormAlias() function does:

    • Use the reflect.Type on obj to get the reflect.Type value.
    • Then call .FieldByName() to get the reflect.Value object from selected field name.
    • The tag information is available through .Tag property. Use that to get the tag value of gorm.
    评论

报告相同问题?

悬赏问题

  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效
  • ¥15 悬赏!微信开发者工具报错,求帮改
  • ¥20 wireshark抓不到vlan
  • ¥20 关于#stm32#的问题:需要指导自动酸碱滴定仪的原理图程序代码及仿真
  • ¥20 设计一款异域新娘的视频相亲软件需要哪些技术支持