【问题标题】:Rust/Rocket/Diesel - How can I query a MySqlDatabase using rocket_sync_db_poolsRust/Rocket/Diesel - 如何使用 rocket_sync_db_pools 查询 MySql 数据库
【发布时间】:2022-09-23 15:57:19
【问题描述】:

当尝试在 Rocket 应用程序中调用由柴油创建的价格模式时,我收到错误消息特征 LoadConnection 未针对 &mut rocket_sync_db_pools::diesel::MysqlConnection 实现

我查看了一些教程,并遵循了 github 和 Rocket\'s 文档以及 Diesel\'s 文档上的示例。

我已经尝试使用diesel::MySqlConnection 而不是rocket_sync_db_pools 来实现DbConn,但我收到一条错误消息特征绑定diesel::MysqlConnection: Poolable 不满足

希望得到一些帮助!

我的代码可以在下面找到。 [main.rs]

#[macro_use] extern crate rocket;
#[macro_use] extern crate diesel;
#[macro_use] extern crate diesel_migrations;
#[macro_use] extern crate rocket_sync_db_pools;


mod schema;
mod db_utils;

use rocket::{Rocket, Build};
use rocket::fairing::AdHoc;
use rocket::request::FlashMessage;
use rocket::serde::Serialize;
use rocket::form::Form;
use rocket::fs::{FileServer, relative};
use diesel::prelude::*;


#[database(\"my_db\")]
pub struct DbConn(rocket_sync_db_pools::diesel::MysqlConnection);


#[get(\"/\")]
fn index(conn: DbConn) {

    conn.run(|c| self::schema::prices::dsl::prices.load(&mut c));

}



#[launch]
fn rocket() -> _ {
    rocket::build()
        .attach(DbConn::fairing())
        .mount(\"/\", routes![index])
}

和 [架构.rs]

// @generated automatically by Diesel CLI.

diesel::table! {
    prices (id) {
        id -> Integer,
        date -> Text,
        ticker -> Text,
        price -> Float,
    }
}

和 [db_utils/models.rs]

use diesel::prelude::*;


#[derive(Queryable)]
pub struct Price {
  pub id: i32,
  pub date: String,
  pub ticker: String,
  pub price: f32
}

    标签: rust rust-diesel rust-rocket rocket rust-diesel-mysql


    【解决方案1】:

    您的问题缺少有关您的依赖版本的重要信息。错误消息和代码示例表明您依赖于rocket 0.5.0-rc.2 和diesel 2.0.0。这些版本不兼容,因为 rocket_sync_db_pools 仅包含对柴油 1.4 的支持。您要么需要使用兼容的柴油版本,要么为柴油 2.0 提供自己的火箭集成

    【讨论】:

      猜你喜欢
      • 2017-09-02
      • 2020-05-18
      • 2020-05-03
      • 2019-04-18
      • 2020-07-17
      • 1970-01-01
      • 2020-07-05
      • 2020-11-22
      • 2020-02-03
      相关资源
      最近更新 更多