gpt4 book ai didi

postgresql - 使用 IN 的 Rails PostgreSQL 语法错误

转载 作者:行者123 更新时间:2023-11-29 13:53:32 26 4
gpt4 key购买 nike

我正在 Rails 应用程序中测试以下代码:

a = Ex.all.pluck(:id)
b = Ex.where("name LIKE ?", "%asdf%").where("id IN ?", a).pluck(:id)

它正在生成这个 PostgreSQL 语句:

SELECT "exs"."id" FROM "exs" WHERE (name LIKE '%asdf%') AND (id IN 9,10,11,12,13,14,15)

但是报错:

C:/Ruby22-x64/lib/ruby/gems/2.2.0/gems/activerecord-4.2.5/lib/active_record/connection_adapters/postgresql_adapter.rb:592:in `async_exec':
PG::SyntaxError: ERROR: syntax error at or near "9" (ActiveRecord::StatementInvalid)
LINE 1: "exs" WHERE (name LIKE '%asdf%') AND (id IN 9,10,11,12...
^

出了什么问题?这是我第一次使用这种查询,我一直无法在任何指南或类似问题中找到答案。

感谢您的宝贵时间。

最佳答案

使用IN时,需要将选项括在括号中:

.where("id IN (?)", a)

关于postgresql - 使用 IN 的 Rails PostgreSQL 语法错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36411635/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com