I'm trying to add a postgres database to a rocket app using diesel. My main.rs
file looks like this, but gives the error "the trait diesel::Connection
is not implemented for DbConnection
" at .get_result(connection)
#[macro_use] extern crate diesel;
extern crate dotenv;
#[macro_use] extern crate rocket;
#[macro_use] extern crate rocket_contrib;
use diesel::prelude::*;
use rocket_contrib::database;
use rocket_contrib::json::JsonValue;
mod models;
mod schema;
use self::models::*;
use self::schema::*;
#[database("my_db")]
struct DbConnection(diesel::PgConnection);
#[get("/")]
fn index(connection: DbConnection) -> JsonValue {
json!(all_bicycles(&connection))
}
fn create_bicycle<'a>(connection: &DbConnection, make: &'a str, model: &'a str, rider_type: &'a str, size: &'a str) -> Bicycle {
let new_bicycle = NewBicycle {
make,
model,
rider_type,
size
};
diesel::insert_into(bicycles::table)
.values(new_bicycle)
// the error is on the following line, on `connection`
.get_result(connection)
.expect("Error saving bicycle")
}
fn main() {
rocket::ignite()
.attach(DbConnection::fairing())
.mount("/", routes![index])
.launch();
}
My Cargo.toml
(the relevant parts)
[dependencies]
diesel = { version = "1.4.4", features = ["postgres"] }
dotenv = "0.15.0"
rocket = { git = "https://github.com/SergioBenitez/Rocket" }
serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0"
[dependencies.rocket_contrib]
git = "https://github.com/SergioBenitez/Rocket"
default-features = false
features = ["json", "diesel_postgres_pool"]
and my Rocket.toml
:
[global.databases]
my_db = { url = "postgres://postgres:@localhost/bikes" }
The error looks like this when expanded:
&DbConnection
the trait bound `DbConnection: diesel::Connection` is not satisfied
the trait `diesel::Connection` is not implemented for `DbConnection`
I've managed to establish the connection to the database, and diesel setup
was successful. I'm also able to add migrations - although I think they're unnecessary for this issue.
What am I doing wrong here?
Edit
I worked through the Rocket docs again and realised I was missing the line use rocket_contrib::databases::diesel;
, which conflicts with extern crate diesel;
, so I moved the database logic into a new module - database.rs
. Nothing has really changed, but the new module looks like this:
use rocket_contrib::database;
use rocket_contrib::databases::diesel;
#[database("my_db")]
pub struct DbConnection(diesel::PgConnection);
and it's being used like so:
main.rs
// ...
mod database;
use self::database::DbConnection;
// ...
The error remains the same.