rust - &diesel::MysqlConnection 未实现特性 diesel::Connection

标签 rust rust-diesel

我正在努力学习 Diesel以下 this tutorial .这是我使用 Diesel 创建的演示程序:

#![recursion_limit = "128"]

#[macro_use]
extern crate diesel;
#[macro_use]
extern crate diesel_infer_schema;

extern crate dotenv;
use diesel::mysql::MysqlConnection;
use diesel::prelude::*;
use dotenv::dotenv;
use std::env;

pub fn establish_connection() -> MysqlConnection {
    dotenv().ok();
    let db_url: String = String::from(env::var("DB_URL").expect("DB_URL must be set"));
    let db_connection =
        MysqlConnection::establish(&db_url).expect(&format!("Error connecting to {}", &db_url));

    return db_connection;
}

pub mod schema {
    infer_schema!("dotenv:DB_URL");
}

use schema::*;

table! {
    tag {
        id -> SmallInt,
        tag_name -> Varchar,
    }
}

#[derive(Queryable, Insertable)]
#[table_name = "tag"]
pub struct Tag {
    pub id: i16,
    pub tag_name: String,
}

fn read_and_output(db_connection: &MysqlConnection) {
    let results = tag::table.load::<Tag>(&db_connection).expect("problem");
    println!("Returned results: {}", results.len());
    for r in results {
        println!("{} {}", r.id, r.tag_name);
    }
}

pub fn insert_tag(db_connection: &MysqlConnection, tag_id_val: i16, tag_name_val: String) {
    let new_tag = Tag {
        id: tag_id_val,
        tag_name: tag_name_val,
    };

    diesel::insert_into(tag::table)
        .values(&new_tag)
        .execute(db_connection)
        .expect("Error inserting");
}

fn main() {
    println!("Hello, world!");
    let db_connection = establish_connection();

    // 1. query data from the table
    //read_and_output(&db_connection);

    // 2. insert new data into the table
    let tag_id: i16 = 778;
    let tag_name: String = String::from("educational");
    insert_tag(&db_connection, tag_id, tag_name);
    //read_and_output(&db_connection);
}

完整代码可在my GitHub repository中找到.

当我使用 cargo build 编译程序时,我得到这个错误:

error[E0277]: the trait bound `&diesel::MysqlConnection: diesel::Connection` is not satisfied
--> src/main.rs:45:30
   |
45 |     let results = tag::table.load::<Tag>(&db_connection)
   |                              ^^^^ the trait `diesel::Connection` is not implemented for `&diesel::MysqlConnection`
   |
= help: the following implementations were found:
            <diesel::MysqlConnection as diesel::Connection>
= note: required because of the requirements on the impl of `diesel::query_dsl::LoadQuery<&diesel::MysqlConnection, Tag>` for `tag::table`

当我注释掉 read_and_output 函数及其调用时,代码会编译并将记录插入数据库。

我不确定如何解决这个错误;看起来我需要实现一些特征,但不确定我该怎么做。

最佳答案

在您的代码中,db_connection 已经是对 MysqlConnection 的引用,即 &MysqlConnection 类型,因此只需传递 db_connection 而不是像这样的 &db_connection

let results = tag::table.load::<Tag>(db_connection);

关于rust - &diesel::MysqlConnection 未实现特性 diesel::Connection,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49593076/

相关文章:

postgresql - 特性 `diesel::Expression` 没有为 `bigdecimal::BigDecimal` 实现

rust - 为什么 Ord 特征不能使用 cmp 函数为继承特征的所需方法提供默认实现?

build - 如何让 Cargo 执行构建脚本并同时使用目标特定的链接器?

singleton - 如何让 Rust 单例的析构函数运行?

rust diesel-cli 为不同的环境设置多个 env 文件

rust - serde_json到json在柴油数据库对象中打印附加字符串\r和\n

mysql - 无法使用柴油箱从 mysql 数据库加载结果

c++ - 如何使用 cxx crate 调用 C++ 构造函数?

rust - 如何对具有泛型类型参数的特征进行装箱?

rust - 在用户不使用N + 1查询的情况下登录时将数据添加到Diesel结果中