简体   繁体   English

如何正确扫描Golang中array_agg function的结果?

[英]How to properly scan the results of the array_agg function in Golang?

In my Golang (1.15) application I use sqlx package to work with the PostgreSQL database (PostgreSQL 12.5).在我的Golang (1.15) 应用程序中,我使用sqlx package 来处理PostgreSQL数据库 (PostgreSQL 12.5)。

My SQL request has an array_agg function that returns the array of strings or null if it's empty.我的SQL请求有一个array_agg function 返回字符串数组或 null 如果它是空的。

I am trying to Scan the results of this SQL request but it raises the next error in my program:我正在尝试Scan此 SQL 请求的结果,但它会在我的程序中引发下一个错误:

sql: Scan error on column index 3, name "organization_ids": unsupported Scan, storing driver.Value type string into type *[]string sql:列索引 3 上的扫描错误,名称“organization_ids”:不支持扫描,将 driver.Value 类型字符串存储到类型 *[]string

Code snippet :代码片段

type Channel struct {
    ChannelId           *string   `db:"channel_id" json:"channelId"`
    ChannelName         *string   `db:"channel_name" json:"channelName"`
    OrganizationsIds    *[]string `db:"organizations_ids" json:"organizationsIds"`
}

var channel Channel

row := db.QueryRow(`
    select
        channels.channel_id::text,
        channels.channel_name::text,
        array_agg(distinct channels_organizations_relationship.organization_id)::text[] organizations_ids
    from
        channels
    left join channels_organizations_relationship on
        channels.channel_id = channels_organizations_relationship.channel_id
    where
        channels.channel_id = $1
    group by
        channels.channel_id
    limit 1;`, *channelId)

if err := row.Scan(&channel.ChannelId, &channel.ChannelName, &channel.OrganizationsIds); err != nil {
    fmt.Println(err)
}

return &channel

I also tried to change the data type of the OrganizationsIds field in the Channel struct to *pg.StringArray from the github.com/lib/pq package.我还尝试将Channel结构中的OrganizationsIds字段的数据类型从github.com/lib/pq package 更改为*pg.StringArray pg.StringArray。 In this case, when I Scan , I get the following error in my program:在这种情况下,当我Scan时,我的程序中出现以下错误:

sql: Scan error on column index 3, name "organizations_ids": unsupported Scan, storing driver.Value type string into type *[]pq.StringArray sql:列索引 3 上的扫描错误,名称“organizations_ids”:不支持扫描,将 driver.Value 类型字符串存储到类型 *[]pq.StringArray

My task is to return a list of strings or null/nil to the client for this column.我的任务是为该列向客户端返回字符串列表或 null/nil。

Can someone explain how to fix this strange behavior?有人可以解释如何解决这种奇怪的行为吗?

Well, finally I found the solution of my problem.好吧,终于我找到了我的问题的解决方案。

I noticed that the array_agg function returns the [null] .我注意到array_agg function 返回[null] I changed the SQL request a little bit to return the null instead of [null] .我稍微更改了 SQL 请求以返回null而不是[null] I make it with this changes:我通过以下更改做到了:

array_agg(distinct channels_organizations_relationship.organization_id) filter (where channels_organizations_relationship.organization_id is not null)

In this article ( Postgres returns [null] instead of [] for array_agg of join table ), you can find many different solutions to this problem.在这篇文章中( Postgres 返回 [null] 而不是 [] for array_agg of join table ),您可以找到许多不同的解决方案来解决这个问题。

Then you can use one of this solutions:然后您可以使用以下解决方案之一:

The 1 option 1 选项

We can use OrganizationsIds pq.StringArray in the struct, and then when scanning do this row.Scan(..., &channel.OrganizationIds .我们可以在结构中使用OrganizationsIds pq.StringArray ,然后在扫描时执行此row.Scan(..., &channel.OrganizationIds

The 2 option 2选项

We can use OrganizationsIds []string in the struct, and then when scanning do this row.Scan(..., (*pq.StringArray)(&channel.OrganizationIds)) .我们可以在结构中使用OrganizationsIds []string ,然后在扫描时执行此row.Scan(..., (*pq.StringArray)(&channel.OrganizationIds))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM