&diesel::MysqlConnection 没有实现特征diesel::Connection
Posted
技术标签:
【中文标题】&diesel::MysqlConnection 没有实现特征diesel::Connection【英文标题】:The trait diesel::Connection is not implemented for &diesel::MysqlConnection 【发布时间】:2018-09-10 14:18:15 【问题描述】:我正在尝试跟随this tutorial 学习Diesel。这是我使用 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
函数及其调用时,代码编译并将记录插入到数据库中。
我不确定如何解决此错误;看起来我需要实现一些特征,但不知道该怎么做。
【问题讨论】:
Why is a trait not implemented for a type that clearly has it implemented? 回答你的问题了吗? Why do I get the error “the traitFoo
is not implemented for &mut T
” even though T implements the trait? 怎么样,我猜如果你删除 &
它会起作用:tag::table.load::<Tag>(db_connection)
【参考方案1】:
在您的代码中db_connection
已经是对MysqlConnection
的引用,即&MysqlConnection
类型,所以只需传递db_connection
而不是&db_connection
就像这样
let results = tag::table.load::<Tag>(db_connection);
【讨论】:
以上是关于&diesel::MysqlConnection 没有实现特征diesel::Connection的主要内容,如果未能解决你的问题,请参考以下文章
要求的参考折叠规则的简明解释:(1) A& & -> A& , (2) A& && -> & , (3) && &a
Java String 将 '&' 替换为 & 而不是 & 到 &
Oracle基本语法&&函数&&子查询&&分页查询&&排序&&集合操作&&高级分组函数