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配列内の整数変数でオブジェクトを簡単に見つけることができますか?
ありがとう!
ActiveRecordを使用してpostgres配列でクエリを実行する方法は複数あります。
クエリの修正:
ip = 1
Notification.where("uip @> '{?}'", ip)
'ANY'の使用:
ip = 1
Notification.where("uip && ARRAY[?]", ip)
この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加