0
0

More than 1 year has passed since last update.

【Rails】Herokuにデプロイしたら検索機能(LIKE関数)が使えなかった【PostgreSQL】

Last updated at Posted at 2022-07-21

環境

Ruby: 2.6.5
Ruby on Rails: 5.2.4.2
heroku: 7.60.2
PostgreSQL: 14.1

開発環境
Cloud9
SQLite3: 3.7.17

問題

Cloud9で開発後、herokuにデプロイし、ECサイトの商品検索機能を使ったところ、次の画面に遷移しました。
スクリーンショット 2022-07-19 18.34.10.png

We're sorry, but something went wrong.
If you are the application owner check the logs for more information.

開発環境では動いていたのに、、、

原因

heroku logs -tコマンドでログを見ると、以下のエラーとヒントが吐かれていました。

ERROR:  operator does not exist: bigint ~~ unknown
HINT:  No operator matches the given name and argument types. You might need to add explicit type casts.

型のキャストをする必要があるようです。
調べてみると、PostgreSQL8.3から、自動キャストがなくなったということです。
参考:PostgreSQL: Documentation

今回はLIKE関数を使っている部分にint型のカラムがあり、text型にキャストできなくてエラーになったようです。

解決

ドキュメントに書いてある通り、CAST ( expression AS type )という書き方をし、無事解決することができました。

- scope :search_carts_by_ids, -> (ids) { where("id LIKE ?", "%#{ids}%") }
+ scope :search_carts_by_ids, -> (ids) { where("cast(id as text) LIKE ?", "%#{ids}%") }
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0