0

我在 Postgres DB 中有下表:

CREATE TABLE resources_index (
    hash varchar NOT NULL,
    "name" varchar NOT NULL,
    viewentry_id varchar NOT NULL,
    value bytea NULL
);

我想使用以下准备好的语句sqlx

SELECT
      t0.name,
      t0.value
    FROM
      (
        SELECT
          name,
          value
        FROM
          resources_index
        WHERE
          hash = $1
          AND value :: BYTEA LIKE $2 :: BYTEA
      ) AS t0
    LIMIT
      $3

问题出现在以下行: AND value :: BYTEA LIKE $2 :: BYTEA

Postgres DB 错误响应是: ERROR: operator does not exist: character varying = bytea

查询执行如下:

let mut q = sqlx::query_as::<sqlx::Postgres, IndexRow>(&query);
    for v in values.iter() {
        log::debug!("bind value {}", v);
        q = q.bind(v.as_bytes());
    }
    q.bind(limit).fetch_all(pool).await

对我来说,它看起来像是v被铸造成的,VARCHAR虽然它应该是BYTEA

你知道这是一个错误还是我做错了?

4

1 回答 1

0

使用内置的解码功能解决了它。

SELECT
      t0.name,
      t0.value
    FROM
      (
        SELECT
          name,
          value
        FROM
          resources_index
        WHERE
          hash = $1
          AND value LIKE decode($2, 'escape')
      ) AS t0
    LIMIT
      $3
于 2021-07-08T05:41:52.470 回答