没有为`diesel_geography :: types :: GeogPoint`实现特征'serde :: Deserialize <'_>`

时间:2019-06-20 16:09:24

标签: rust postgis serde rust-diesel

我正在尝试使用Diesel和diesel_geography使用Rust从PostGIS数据库中读取。

这是我遇到的错误:

error[E0277]: the trait bound `diesel_geography::types::GeogPoint: serde::Serialize` is not satisfied
  --> src/models.rs:11:5
   |
11 |     pub coordinates: GeogPoint,
   |     ^^^ the trait `serde::Serialize` is not implemented for `diesel_geography::types::GeogPoint`
   |
   = note: required by `serde::ser::SerializeStruct::serialize_field`

error[E0277]: the trait bound `diesel_geography::types::GeogPoint: serde::Deserialize<'_>` is not satisfied
  --> src/models.rs:11:5
   |
11 |     pub coordinates: GeogPoint,
   |     ^^^ the trait `serde::Deserialize<'_>` is not implemented for `diesel_geography::types::GeogPoint`
   |
   = note: required by `serde::de::SeqAccess::next_element`

error[E0277]: the trait bound `diesel_geography::types::GeogPoint: serde::Deserialize<'_>` is not satisfied
  --> src/models.rs:11:5
   |
11 |     pub coordinates: GeogPoint,
   |     ^^^ the trait `serde::Deserialize<'_>` is not implemented for `diesel_geography::types::GeogPoint`
   |
   = note: required by `serde::de::MapAccess::next_value`

环顾四周,我发现当有多个版本的serde用作依赖项时,会发生类似的错误,可以使用cargo tree -d进行检查。我已经尝试过,serde不会显示为重复的依赖项。

到目前为止,这是我的代码:

Cargo.toml

[package]
name = "123"
version = "0.1.0"
authors = ["ASD"]
edition = "2018"

[dependencies]
diesel = { version = "1.4.2", features = ["postgres"] }
serde = { version = "1.0", features = ["derive"] } 
serde_json="1.0"

dotenv = "0.14.1"

diesel-geography = "0.2.0"

schema.rs

table! {
    use diesel::sql_types::*;
    use diesel_geography::sql_types::*;

    users (email) {
        email -> Varchar,
        password -> Varchar,
        coordinates -> Geography
    }
}

models.rs

use diesel_geography::types::*;
use crate::schema::users;
use serde::{Serialize, Deserialize};

#[derive(Debug, Serialize, Deserialize, Queryable, Insertable)]
#[table_name = "users"]
pub struct User {
    pub email: String,
    pub password: String,
    pub coordinates: GeogPoint
}

main.rs

extern crate serde;
extern crate dotenv;
#[macro_use] extern crate diesel;
//#[macro_use] extern crate serde_derive;

mod models;
mod schema;
use diesel::PgConnection;
use dotenv::dotenv;
use std::env;

use diesel::prelude::*;

fn main() {
    dotenv().ok();

    let database_url = env::var("DATABASE_URL")
        .expect("DATABASE_URL must be set");

    let connection = PgConnection::establish(&database_url)
        .expect(&format!("Error connecting to {}", database_url));

    use crate::schema::users::dsl::*;
    use crate::models::User;

    let results = users
        .limit(5)
        .load::<User>(&connection)
        .expect("Error loading users");

    println!("Displaying {} users", results.len());

    for user in results {
        println!("{}", user.email);
        println!("----------\n");
        println!("{}", user.password);
    }
}

1 个答案:

答案 0 :(得分:0)

Serde是optional dependency of diesel-geography。您需要启用该功能:

[dependencies]
diesel-geography = { version = "0.2.0", features = ["serde"] }