Gorm只能通过使用Find方法获取一条记录。

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

Gorm only get only one records by using Find

问题

我正在尝试从我的数据库中获取所有记录,使用Find方法来获取符合条件的记录,以下是我的代码:

result := map[string]interface{}{}

conn = conn.Table("routeName")
conn = conn.Where("status = ?", 1)
conn = conn.Find(&result).Debug()

fmt.Println(result)

由于使用了interface,我只得到了一行记录,结果如下:

map[id:1 routeName:FAFA status:1 routeCode:A]

我以为是interface的问题,所以我尝试使用数组作为结果,以下是另一种定义的结果:

var result []model.RouteName

使用这种方式,我得到了以下记录:

{[0 0][0 0]}

我发现我的记录数据没有放入result中,为了确认我确实从数据库中获取了行记录,我使用count函数计算记录数,结果为2,所以我认为我确实获取了记录,但是无法正确获取记录的值。以下是我的模型定义:

type RouteName struct {
    id        int    `gorm:"column:id" json:"id"`
    routeName string `gorm:"column:routeName" json:"routeName"`
    status    int    `gorm:"column:status" json:"status"`
    routeCode string `gorm:"column:routeCode" json:"routeCode"`
}

func (RouteName) TableName() string {
    return "routeName"
}

我不知道为什么会出现这种情况,需要一些建议来解决它。

英文:

I'm trying to get all records from my DB,
using Find to get condition records,
here's my code

result := map[string]interface{}{}

conn = conn.Table("routeName")
conn = conn.Where("status = ?", 1)
conn = conn.Find(&result).Debug()

fmt.Println(result)

As using interface, I only get one row, result as following

map[id:1 routeName:FAFA status:1 routeCode:A]

I thought it's my interface's problem, so I tried using array as result,
here's antoher define result

var result []model.RouteName

with this, I got following records

{[0    0][0    0]}

seen that my records data didn't put into result, to comfire that I did get row records from DB, Using count func to count records and result get 2, so I think that I do get records, but somehow I can't correctly get records value, following is my model

type RouteName struct {
    id           int    `gorm:"column:id" json:"id"`
    routeName    string `gorm:"column:routeName" json:"routeName"`
    status       int    `gorm:"column:status" json:"status"`
    routeCode    string `gorm:"column:routeCode" json:"routeCode"`
}

func (RouteName) TableName() string {
    return "routeName"
}

I have no idea that why it's work like this, need some advice to fix it.

答案1

得分: 4

Gorm无法访问您的结构体中的字段,因为它们没有被导出。

在之前的代码中,将字段名的首字母改为大写即可解决该问题。

修改后的代码如下:

type RouteName struct {
    Id           int    `gorm:"column:id" json:"id"`
    RouteName    string `gorm:"column:routeName" json:"routeName"`
    Status       int    `gorm:"column:status" json:"status"`
    RouteCode    string `gorm:"column:routeCode" json:"routeCode"`
}

请注意,我已经将代码中的双引号从"替换为了实际的双引号。

英文:

Gorm cannot access fields in your struct because they are not exported.
> Exported identifiers
>
> An identifier may be exported to permit access to it from another package.
> An identifier is exported if both:
> - the first character of the identifier's name is a Unicode upper case letter (Unicode class "Lu"); and
> - the identifier is declared in the package block or it is a field name or method name.
>
> All other identifiers are not exported.

Before

type RouteName struct {
id           int    `gorm:"column:id" json:"id"`
routeName    string `gorm:"column:routeName" json:"routeName"`
status       int    `gorm:"column:status" json:"status"`
routeCode    string `gorm:"column:routeCode" json:"routeCode"`
}

After

type RouteName struct {
    Id           int    `gorm:"column:id" json:"id"`
    RouteName    string `gorm:"column:routeName" json:"routeName"`
    Status       int    `gorm:"column:status" json:"status"`
    RouteCode    string `gorm:"column:routeCode" json:"routeCode"`
}

答案2

得分: 0

GORM将使用反射来映射每个导出的结构标签,并将其写入相应的字段中。而未导出的字段将在此处被提前过滤掉。


我认为你应该尝试公开你的结构字段。

type RouteName struct {
	ID           int    `gorm:"column:id" json:"id"`
	RouteName    string `gorm:"column:routeName" json:"routeName"`
	Status       int    `gorm:"column:status" json:"status"`
	RouteCode    string `gorm:"column:routeCode" json:"routeCode"`
}
英文:

(Edit) Add some description for this answer.

GORM will use reflection to map each exported struct tag and
write into the corresponding field.
And unexported fields will be filtered out earlier here


I think you should try to publicize your struct fields.

type RouteName struct {
	ID           int    `gorm:"column:id" json:"id"`
	RouteName    string `gorm:"column:routeName" json:"routeName"`
	Status       int    `gorm:"column:status" json:"status"`
	RouteCode    string `gorm:"column:routeCode" json:"routeCode"`
}

huangapple
  • 本文由 发表于 2022年4月19日 15:17:44
  • 转载请务必保留本文链接:https://go.coder-hub.com/71921010.html
匿名

发表评论

匿名网友

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

确定