'how to do a jsonb query in rust diesel

I am store some tags as a jsonb datatype in PostgreSQL 13, this is the table DDL:

-- Drop table

-- DROP TABLE public.test;

CREATE TABLE public.test (
    id int8 NOT NULL GENERATED ALWAYS AS IDENTITY,
    tags jsonb NOT NULL,
    CONSTRAINT test_pkey PRIMARY KEY (id)
);

now I want to do a query using jsonb in rust diesel diesel = { version = "1.4.7", features = ["postgres","serde_json"] }, this is the rust main.rs:

#[macro_use]
extern crate diesel;

use diesel::pg::types::sql_types::Jsonb;
use diesel::{ExpressionMethods, QueryDsl, RunQueryDsl};
use rocket::serde::Deserialize;
use rocket::serde::Serialize;
use rust_wheel::config::db::config::establish_connection;

pub mod model;

fn main() {
    use crate::model::diesel::dict::dict_schema::test::dsl::*;
    let connection = rust_wheel::config::db::config::establish_connection();
    let predicate = crate::model::diesel::dict::dict_schema::test::tags.eq(serde_json::from_value("value".parse().unwrap()));
    let db_admin_user = test.filter(&predicate)
        .limit(1)
        .load::<Test>(&connection)
       .expect("query test failed");
}

#[derive(Queryable,Debug,Serialize,Deserialize,Default)]
pub struct Test {
    pub id: i64,
    pub tags: serde_json::Value,
}

and this is the schema file:

table! {
    test (id) {
        id -> Int8,
        tags -> Jsonb,
    }
}

when I compile the code, shows error like this:

error[E0277]: the trait bound `Result<_, serde_json::Error>: diesel::Expression` is not satisfied
  --> src/main.rs:15:76
   |
15 |     let predicate = crate::model::diesel::dict::dict_schema::test::tags.eq(serde_json::from_value("value".parse().unwrap()));
   |                                                                         -- ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ the trait `diesel::Expression` is not implemented for `Result<_, serde_json::Error>`
   |                                                                         |
   |                                                                         required by a bound introduced by this call
   |
   = note: required because of the requirements on the impl of `AsExpression<diesel::sql_types::Jsonb>` for `Result<_, serde_json::Error>`

what should I do to query the record using jsonb data type?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source