【问题标题】:How do you query postgres arrays in ruby on rails for integers using a variable?如何使用变量在 ruby​​ on rails 中查询 postgres 数组以获取整数?
【发布时间】:2015-02-10 12:30:56
【问题描述】:
Notification.where("uip  @> ?", '{1}')

工作正常并返回所有 uip 数组包含 1 的通知。

但是,如果我使用变量尝试以下操作,我就没有这样的运气:

ip = 1

Notification.where("uip  @> ?", '{ip}')

返回错误:

Notification Load (1.8ms)  SELECT "notifications".* FROM "notifications"  WHERE (uip  @> '{ip}')
PG::InvalidTextRepresentation: ERROR:  invalid input syntax for integer: "ip"
LINE 1: ...otifications".* FROM "notifications"  WHERE (uip  @> '{ip}')
                                                                ^
: SELECT "notifications".* FROM "notifications"  WHERE (uip  @> '{ip}')
ActiveRecord::StatementInvalid: PG::InvalidTextRepresentation: ERROR:  invalid input syntax for integer: "ip"
LINE 1: ...otifications".* FROM "notifications"  WHERE (uip  @> '{ip}')
                                                                ^
: SELECT "notifications".* FROM "notifications"  WHERE (uip  @> '{ip}')

又一次尝试:

Notification.where("uip  @> ?", ip)

给出错误:

SELECT "notifications".* FROM "notifications"  WHERE (uip  @> 1)
PG::UndefinedFunction: ERROR:  operator does not exist: bigint[] @> integer
LINE 1: ...CT "notifications".* FROM "notifications"  WHERE (uip  @> 1)
                                                                  ^
HINT:  No operator matches the given name and argument type(s). You might need to add explicit type casts.
: SELECT "notifications".* FROM "notifications"  WHERE (uip  @> 1)
ActiveRecord::StatementInvalid: PG::UndefinedFunction: ERROR:  operator does not exist: bigint[] @> integer
LINE 1: ...CT "notifications".* FROM "notifications"  WHERE (uip  @> 1)
                                                                  ^
HINT:  No operator matches the given name and argument type(s). You might need to add explicit type casts.
: SELECT "notifications".* FROM "notifications"  WHERE (uip  @> 1)

那么我怎样才能通过 Rails 中的 postgres 数组中的整数变量简单地找到对象呢?

谢谢!

【问题讨论】:

  • 代替Notification.where("uip @> ?", '{ip}')试试Notification.where("uip @> ?", '{' + ip + '}')

标签: ruby-on-rails ruby arrays postgresql ruby-on-rails-4


【解决方案1】:

您可以通过多种方式使用 ActiveRecord 在 postgres 数组中进行查询:

修正您的查询:

ip = 1    
Notification.where("uip @> '{?}'", ip)

使用“任何”:

ip = 1
Notification.where("uip && ARRAY[?]", ip)

【讨论】:

  • 第一个工作谢谢:),第二个似乎没有:PG::UndefinedFunction: ERROR: operator does not exist: bigint[] && integer[]
  • 我建议您使用@>,因为您可以使用 gin 对其进行索引。
  • 第一个似乎不适用于字符串数组,但第二个可以
【解决方案2】:

您需要根据输入创建一个数组:

Notification.where("uip @> CAST(ARRAY[?] AS BIGINT[])", ip)
// or
Notification.where("uip @> CAST(? AS BIGINT[])", '{' + ip.to_s + '}')
// or
Notification.where("uip @> CAST('{' || ? || '}' AS BIGINT[])", ip)

如果您只想测试一个元素,您也可以使用重叠 (&&) 运算符(应该更快一点)。或者,您可以对数组使用 ANY 构造:

Notification.where("? = ANY (uip)", ip)

【讨论】:

  • 第三和第四个工作谢谢:),前两个渲染错误:分别为PG::UndefinedFunction: ERROR: operator does not exist: bigint[] @> integer[]TypeError: no implicit conversion of Fixnum into String
  • ANY 会比 @> 快吗?
  • @Laser 那么你需要到处进行显式转换; ANY 更简单,适用于较小的集合,但不可索引。 @>&& 运算符可使用 gin 进行索引。
【解决方案3】:

试试这个。我刚刚把你的牙套移到了状态:

ip = 1
Notification.where("uip  @> '{?}'", ip)

【讨论】:

  • PG::SyntaxError: ERROR: syntax error at or near "{"
【解决方案4】:

使用它。我认为这对你有帮助

ip = 1 
Notification.where("uip  @> ?", "#{ip.to_s.to_i}") 

Notification.where("uip @>?",ip.to_s.to_i)

【讨论】:

  • PG::InvalidTextRepresentation: ERROR: malformed array literal: "1"
  • 你好激光,我现在改了答案..你用这个方法..我觉得对你会有帮助
猜你喜欢
  • 2019-01-08
  • 1970-01-01
  • 2015-07-04
  • 2020-07-21
  • 2016-05-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-11-24
相关资源
最近更新 更多