扫描gorm查询的结构体

huangapple go评论97阅读模式
英文:

Scanning into struct of gorm query

问题

我正在尝试将查询结果扫描到一个res结构中。

代码构建成功,查询也通过了,但是结果数组中包含了默认值,如下所示:

[{0 0 0} {0 0 0} {0 0 0} {0 0 0} {0 0 0} {0 0 0}]

此外,结果数组的长度与查询结果应该具有的长度完全相同。
当我在Postgres shell中尝试生成的查询时,返回的结果是正确的。

代码:

type res struct{
    id int
    number int
    user_id int
}

func getDataJoin(){
    new := []res{}
    db.Db.Table("users").Select("users.id as id, credit_cards.number as number, credit_cards.user_id as user_id").Joins("left join credit_cards on credit_cards.user_id = users.id").Scan(&new)
    fmt.Println("user\n",new)
}

生成的查询:

SELECT users.id as id, credit_cards.number as number, credit_cards.user_id as user_id FROM "users" left join credit_cards on credit_cards.user_id = users.id

数据库结果:

id | number | user_id 
----+--------+---------
  1 | 1      |       1
  1 | 2      |       1
  2 | 1      |       2
  2 | 2      |       2
  3 | 1      |       3
  3 | 2      |       3
(6 rows)
英文:

I'm trying to scan the result of a query into a res structure.

The code builds and the query passes but the result array consists of default values like this:

> [{0 0 0} {0 0 0} {0 0 0} {0 0 0} {0 0 0} {0 0 0}]

Also, result array has the exact length as the query result should have.
When i try generated query in postgres shell it returns the result correctly.

Code:

 type res struct{
    id int
    number int
    user_id int
    }
    
    func getDataJoin(){
    	new := []res{}
    	db.Db.Table("users").Select("users.id as id, credit_cards.number as number, credit_cards.user_id as user_id").Joins("left join credit_cards on credit_cards.user_id = users.id").Scan(&new)
    	fmt.Println("user\n",new)
    }

Generated Query:

SELECT users.id as id, credit_cards.number as number, credit_cards.user_id as user_id FROM "users" left join credit_cards on credit_cards.user_id = users.id

Database result

id | number | user_id 
----+--------+---------
  1 | 1      |       1
  1 | 2      |       1
  2 | 1      |       2
  2 | 2      |       2
  3 | 1      |       3
  3 | 2      |       3
(6 rows)

答案1

得分: 9

由于go-gorm在命名方面有一定的约定,您可能想尝试两件事。

第一种方法是将您的res结构体公开,并使用公共字段:

type Res struct{
    ID int
    Number int
    UserID int
}

第二种方法是指定列和字段之间的映射关系:

type res struct{
    id int      `gorm:"column:id"`
    number int  `gorm:"column:number"`
    user_id int `gorm:"column:user_id"`
}
英文:

Since go-gorm has a certain convention when it comes to naming, you might want to try two things.

Make your res struct publicly available, with public fields:

type Res struct{
    ID int
    Number int
    UserID int
}

Or, specify mappings between columns and fields:

type res struct{
    id int      `gorm:"column:id"`
    number int  `gorm:"column:number"`
    user_id int `gorm:"column:user_id"`
}

答案2

得分: 1

gorm只能读取/写入导出字段,就像json包的Marshal/Unmarshal方法一样。如果字段的第一个字母是大写的,它将被使用。默认情况下,gorm将结构体字段与其驼峰形式进行匹配。您还可以定义自己的列名

由于IDId的驼峰形式都是id,只要字段的第一个字母是大写的,它就应该工作。另外,写ID,即两个字母都大写,是一个好的做法。

英文:

gorm can only read/write on exported fields much like Marshal/Unmarshal methods of json package. If the first letter of your field is in capital, it will be used. By default, gorm matches struct fields with their camel-cased forms. You can also define your own column names.

Since camel-cased form of both ID and Id, is id, as long as the first letter of your field is in capital, it should work. On a different note, it's good practice to write ID, i.e., both letter capital.

huangapple
  • 本文由 发表于 2021年6月23日 21:03:54
  • 转载请务必保留本文链接:https://go.coder-hub.com/68100389.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定