New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Diesel use serde_json::Value for struct field failed #1841

Closed
Raytlty opened this Issue Sep 11, 2018 · 2 comments

Comments

Projects
None yet
2 participants
@Raytlty

Raytlty commented Sep 11, 2018

Setup

Versions

  • Rust: rustc 1.30.0-nightly (0f063aef6 2018-09-03)
  • Diesel: v1.3.2
  • Database: postgres 10.1
  • Operating System: MacOS

Feature Flags

  • diesel: ["postgres", "r2d2", "chrono"]

Problem Description

src/core/person.rs

use ::schema::person;
use serde_json::Value;
use diesel::prelude::*;

#[derive(Debug, AsChangeset, Queryable, Insertable)]
#[table_name = "person"]
pub struct Person {
    pub id: i32,
    pub name: String,
    pub description: Option<Value>,
}

src/schema.rs

table! {
    person (id) {
        id -> Int4,
        name -> Varchar,
        description -> Nullable<Jsonb>,
    }
}

I want to add Insertable and AsChangeset macro to struct Person.
And Person has a Json Field. But it failed.

Does diesel fully support Postgres Json? Thanks.

What are you trying to accomplish?

Use diesel and support json field at the model.

What is the expected output?

I want cargo build can success

What is the actual output?

Raise a lot of errors like

error[E0277]: the trait bound `serde_json::Value: diesel::Expression` is not satisfied
 --> src/core/person.rs:5:41
  |
5 | #[derive(Debug, AsChangeset, Queryable, Insertable)]
  |                                         ^^^^^^^^^^ the trait `diesel::Expression` is not implemented for `serde_json::Value`
  |
  = note: required because of the requirements on the impl of `diesel::expression::AsExpression<diesel::sql_types::Nullable<diesel::sql_types::Jsonb>>` for `serde_json::Value`

error[E0277]: the trait bound `serde_json::Value: diesel::Expression` is not satisfied
 --> src/core/person.rs:5:41
  |
5 | #[derive(Debug, AsChangeset, Queryable, Insertable)]
  |                                         ^^^^^^^^^^ the trait `diesel::Expression` is not implemented for `serde_json::Value`
  |
  = note: required because of the requirements on the impl of `diesel::Expression` for `&serde_json::Value`
  = note: required because of the requirements on the impl of `diesel::expression::AsExpression<diesel::sql_types::Nullable<diesel::sql_types::Jsonb>>` for `&serde_json::Value`

error[E0277]: the trait bound `serde_json::Value: diesel::AppearsOnTable<schema::person::table>` is not satisfied
 --> src/core/person.rs:5:17
  |
5 | #[derive(Debug, AsChangeset, Queryable, Insertable)]
  |                 ^^^^^^^^^^^ the trait `diesel::AppearsOnTable<schema::person::table>` is not implemented for `serde_json::Value`
  |
  = note: required because of the requirements on the impl of `diesel::AppearsOnTable<schema::person::table>` for `&serde_json::Value`
  = note: required because of the requirements on the impl of `diesel::query_builder::AsChangeset` for `diesel::expression::operators::Eq<schema::person::columns::description, &serde_json::Value>`

error[E0277]: the trait bound `serde_json::Value: diesel::AppearsOnTable<schema::person::table>` is not satisfied
 --> src/core/person.rs:5:17
  |
5 | #[derive(Debug, AsChangeset, Queryable, Insertable)]
  |                 ^^^^^^^^^^^ the trait `diesel::AppearsOnTable<schema::person::table>` is not implemented for `serde_json::Value`
  |
  = note: required because of the requirements on the impl of `diesel::query_builder::AsChangeset` for `diesel::expression::operators::Eq<schema::person::columns::description, serde_json::Value>`

error[E0277]: the trait bound `serde_json::Value: diesel::Expression` is not satisfied
 --> src/core/person.rs:5:41
  |
5 | #[derive(Debug, AsChangeset, Queryable, Insertable)]
  |                                         ^^^^^^^^^^ the trait `diesel::Expression` is not implemented for `serde_json::Value`
  |
  = note: required because of the requirements on the impl of `diesel::Expression` for `&'insert serde_json::Value`
  = note: required because of the requirements on the impl of `diesel::expression::AsExpression<diesel::sql_types::Nullable<diesel::sql_types::Jsonb>>` for `&'insert serde_json::Value`

error[E0277]: the trait bound `serde_json::Value: diesel::AppearsOnTable<schema::person::table>` is not satisfied
 --> src/core/person.rs:5:17
  |
5 | #[derive(Debug, AsChangeset, Queryable, Insertable)]
  |                 ^^^^^^^^^^^ the trait `diesel::AppearsOnTable<schema::person::table>` is not implemented for `serde_json::Value`
  |
  = note: required because of the requirements on the impl of `diesel::AppearsOnTable<schema::person::table>` for `&'update serde_json::Value`
  = note: required because of the requirements on the impl of `diesel::query_builder::AsChangeset` for `diesel::expression::operators::Eq<schema::person::columns::description, &'update serde_json::Value>`

Are you seeing any additional errors?

Steps to reproduce

Checklist

  • [* ] I have already looked over the issue tracker for similar issues.
  • [ *] This issue can be reproduced on Rust's stable channel. (Your issue will be
    closed if this is not the case)
@weiznich

This comment has been minimized.

Contributor

weiznich commented Sep 11, 2018

As the docs metion you need to enable the "serde_json" feature to use the conversion form Jsonb to serde_json::Value.

@weiznich weiznich closed this Sep 11, 2018

@Raytlty

This comment has been minimized.

Raytlty commented Sep 12, 2018

@weiznich Much Appreciate! I figured it out yesterday. THX reply.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment