1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Rust sqlx 「operator does not exist: uuid = text」というエラーの対処法

Posted at

原因

SQLを解析している最中に、UUIDと文字列を比較・代入している箇所が発見されている。

※SQLを実行してエラーになっているわけではない。

参考コード

sqlx::query_as::<_, SubscriptionRow>(
            "SELECT * FROM subscriptions WHERE user_id = $1 AND id = $2",
        )
        .bind(&user_id.value) // String型(テーブルはUUID)
        .bind(&subscription_id.value) // String型(テーブルはUUID)
        .fetch_one(pool)
        .await

上記のコードのような場合、実行時に実行前のSQLチェック段階でエラーになる。

下記のように修正するとエラーが解消される。

"SELECT * FROM subscriptions WHERE user_id = $1::uuid AND id = $2::uuid"

ひとこと

以下の理由で原因を探すのがかなり大変でした。

  • コンパイルエラーは発生しない
  • SQL自体は実行可能
  • エラーメッセージの情報が少ない
  • sqlx自体の情報が少ない
1
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
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?