4

我正在尝试使用 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`

环顾四周,我发现当有多个版本的serdeused as 依赖项时会发生类似的错误,这可以使用cargo tree -d. 我已经尝试过并且serde没有显示为重复的依赖项。

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

货运.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"

架构.rs

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

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

模型.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);
    }
}
4

1 回答 1

6

Serde 是柴油地理的可选依赖项。您需要启用该功能:

[dependencies]
diesel-geography = { version = "0.2.0", features = ["serde"] }
于 2019-06-20T16:22:40.420 回答