Golang SQLBoiler在运行orderby时没有返回任何内容。

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

Golang SQLBoiler returns nothing when running orderby

问题

我正在使用Go语言的SqlBoiler库向PostgreSQL数据库发送请求,但是当尝试按照某个字段对数据库进行排序时,返回的结果集为空。

首先,我运行了一个计数查询(如下所示),如果计数返回的行数大于等于1,则查询数据库获取所有结果。

这是返回正确的行数计数:

res, _ := models.MT(
        Where("(mt_mas = ? or mt_mem like ?) and mt_group = ?", uint(uid), `%"`+strconv.Itoa(uid)+`"%`, bool(mt_group_bool)),
    ).Count(CTX, DB)

这是返回空结果集的查询,尽管查询参数完全相同:

res, _ := models.MT(
        Where("(mt_mas = ? or mt_mem like ?) and mt_group = ?", uint(uid), `%"`+strconv.Itoa(uid)+`"%`, bool(mt_group_bool)),
        OrderBy("mt_mas"),
    ).Count(CTX, DB)

这是在检查行数计数后获取所有行的方法:

res, err := models.MT(
        Where("(mt_mas = ? or mt_mem like ?) and mt_group = ?", uint(uid), `%"`+strconv.Itoa(uid)+`"%`, bool(mt_group_bool)),
        OrderBy("mt_mas"),
    ).All(CTX, DB)

当从上述请求中读取错误时,它打印出<nil>,一切似乎都正常。

数据库是PostgreSQL(版本为 PostgreSQL 13.3),列如下:

  • mt_mas(整数)
    该列保存此行所有者的uid。
  • mt_mem(字符变长[1000])
    该列保存用户成员uid的JSON列表。
  • mt_group(布尔值)
    该列显示此行是否为组。

数据库行的示例:

mt_mas mt_mem mt_group
1 {"1", "2"} false
英文:

I'm using SqlBoiler on Go to send requests to a PostgreSQL db, but when trying to order the database by one of the fields it returns 0 rows.

At first i run a count (like detailed below) and if the count returns more than or equal to one row then i query the database for all the results.

This returns the correct row count:

res, _ := models.MT(
        Where(&quot;(mt_mas = ? or mt_mem like ?) and mt_group = ?&quot;, uint(uid), `%&quot;`+strconv.Itoa(uid)+`&quot;%`, bool(mt_group_bool)),
    ).Count(CTX, DB)

This returns no rows, despite the query params being exactly the same:

res, _ := models.MT(
        Where(&quot;(mt_mas = ? or mt_mem like ?) and mt_group = ?&quot;, uint(uid), `%&quot;`+strconv.Itoa(uid)+`&quot;%`, bool(mt_group_bool)),
        OrderBy(`mt_mas`),
    ).Count(CTX, DB)

This is how I get all the rows after checking the row count:

res, err := models.MT(
        Where(&quot;(mt_mas = ? or mt_mem like ?) and mt_group = ?&quot;, uint(uid), `%&quot;`+strconv.Itoa(uid)+`&quot;%`, bool(mt_group_bool)),
        OrderBy(`mt_mas`),
    ).All(CTX, DB)

When reading the error from the request above, it prints out &lt;nil&gt; and everything seems fine.

The database is, as mentioned Postgres (version PostgreSQL 13.3), and the columns are as follows:

  • mt_mas (integer)
    This column holds the uid of the owner of this row.
  • mt_mem (character varying [1000])
    This column holds a JSON list of user members uid:s.
  • mt_group (boolean)
    This column shows of this row is a group of not.

Example of database row:

mt_mas mt_mem mt_group
1 {"1", "2"} false

答案1

得分: 2

好的,以下是翻译好的内容:

好的,解决方案比预期的简单。实际上,@Gari Singh(https://stackoverflow.com/users/5529712/gari-singh)在上面的评论中指出了这一点,但我觉得我会在这里写下来,这样问题就会被标记为已解决。

解决方案就是在计算结果的查询中不排序。所以正确的计数代码应该只是:

res, _ := models.MT(
        Where("(mt_mas = ? or mt_mem like ?) and mt_group = ?", uint(uid), `%"`+strconv.Itoa(uid)+`"%`, bool(mt_group_bool)),
    ).Count(CTX, DB)

然后运行带有排序的查询来获取实际的行,像这样:

res, err := models.MT(
        Where("(mt_mas = ? or mt_mem like ?) and mt_group = ?", uint(uid), `%"`+strconv.Itoa(uid)+`"%`, bool(mt_group_bool)),
        OrderBy("mt_mas"),
    ).All(CTX, DB)

谢谢你的帮助! Golang SQLBoiler在运行orderby时没有返回任何内容。

英文:

Okay, the solution was simpler than expected. @Gari Singh (https://stackoverflow.com/users/5529712/gari-singh) actually pointed this out in the comments above, but i figured i would write it here so the question registers as solved.

The solution is to simply not order in the query that counts the results. So the correct code for counting should only be:

res, _ := models.MT(
        Where(&quot;(mt_mas = ? or mt_mem like ?) and mt_group = ?&quot;, uint(uid), `%&quot;`+strconv.Itoa(uid)+`&quot;%`, bool(mt_group_bool)),
    ).Count(CTX, DB)

And then running the query to get the actual rows with the order, like this:

res, err := models.MT(
        Where(&quot;(mt_mas = ? or mt_mem like ?) and mt_group = ?&quot;, uint(uid), `%&quot;`+strconv.Itoa(uid)+`&quot;%`, bool(mt_group_bool)),
        OrderBy(&quot;mt_mas&quot;),
    ).All(CTX, DB)

Thank you for your help! Golang SQLBoiler在运行orderby时没有返回任何内容。

huangapple
  • 本文由 发表于 2021年7月31日 17:52:55
  • 转载请务必保留本文链接:https://go.coder-hub.com/68600999.html
匿名

发表评论

匿名网友

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

确定