New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

diesel::insert compilation error #1296

Closed
attdona opened this Issue Nov 5, 2017 · 6 comments

Comments

Projects
None yet
3 participants
@attdona

attdona commented Nov 5, 2017

Setup

Versions

  • Rust: rustc 1.22.0-nightly (417c73891 2017-10-05)
  • Diesel: 0.16.0
  • Database: postgres
  • Operating System ubuntu 17.10

Problem Description

This code snippet:

fn add_user(db: DB, message: Json<User>) -> JsonValue {
    use schema::users::dsl::*;


    //diesel::insert(&NewUser {name: &message.name})
    diesel::insert(&name.eq(message.name))
        .into(users)
        .execute(db.conn())
        .expect("Error saving new user");

    json!({ "status": "ok" })
}

Throws the compile error:

error[E0061]: this function takes 0 parameters but 1 parameter was supplied
   --> src/main.rs:136:15
    |
136 |         .into(users)
    |               ^^^^^ expected 0 parameters

error: aborting due to previous error

Using a struct for value (the commented line) rustc compiles.

What is going wrong? I don't understand the error message.

Greetings
Attilio

@weiznich

This comment has been minimized.

Contributor

weiznich commented Nov 7, 2017

diesel::insert currently only supports inserting structs. The error message is indicting that in a really bad way. Rustc assumes one wants to use Into::into instead of IncompleteInsertStatement::into.

On diesels master-branch the story around insert changed drastically. The new insert_into method allows setting values directly and also improves the error message.

@attdona

This comment has been minimized.

attdona commented Nov 8, 2017

Ok, it make sense for me to try the latest diesel.
I've pointed diesel to master-branch, but now this code stop compiling:

#[macro_use]
extern crate diesel_codegen;
#[macro_use]
extern crate diesel;
extern crate dotenv;
extern crate r2d2;
extern crate r2d2_diesel;

pub mod schema;
pub mod models;

use diesel::pg::PgConnection;
use r2d2::{ Pool, Config };
use r2d2_diesel::ConnectionManager;
use dotenv::dotenv;
use std::env;

pub fn create_db_pool() -> Pool<ConnectionManager<PgConnection>> {
    dotenv().ok();

    let database_url = env::var("DATABASE_URL")
        .expect("DATABASE_URL must be set");
    let config = Config::default();
    let manager = ConnectionManager::<PgConnection>::new(database_url);
    Pool::new(config, manager).expect("Failed to create pool.")
}

rustc output:

error[E0277]: the trait bound `diesel::PgConnection: diesel::connection::Connection` is not satisfied
  --> src/lib.rs:18:1
   |
18 | / pub fn create_db_pool() -> Pool<ConnectionManager<PgConnection>> {
19 | |     dotenv().ok();
20 | |
21 | |     let database_url = env::var("DATABASE_URL")
...  |
25 | |     Pool::new(config, manager).expect("Failed to create pool.")
26 | | }
   | |_^ the trait `diesel::connection::Connection` is not implemented for `diesel::PgConnection`
   |
   = note: required because of the requirements on the impl of `r2d2::ManageConnection` for `r2d2_diesel::ConnectionManager<diesel::PgConnection>`
   = note: required by `r2d2::Pool`

The online docs http://docs.diesel.rs/diesel/pg/struct.PgConnection.html states that the Connection trait is implemented by PgConnection.
I probably missing something.

My only Cargo.toml change (original line commented):
diesel = { git="https://github.com/diesel-rs/diesel", features = ["postgres"] }
#diesel = { version = "0.16.0", features = ["postgres"] }

@sgrif

This comment has been minimized.

Member

sgrif commented Nov 8, 2017

You will need to use [replace] in your Cargo.toml, it's not enough to just change your diesel line if you are using r2d2 or other plugins.

http://doc.crates.io/manifest.html#the-replace-section

@attdona

This comment has been minimized.

attdona commented Nov 8, 2017

I tried, but clearly I'm doing it in a wrong way ...

Cargo.toml:

[dependencies]
diesel = { version = "0.16.0", features = ["postgres"] }
...
[replace]
"diesel:0.16.0" = {git="https://github.com/diesel-rs/diesel"}

rustc output:

error[E0599]: no method named `load` found for type `diesel::query_builder::SelectStatement<information_schema::information_schema::key_column_usage::table, diesel::query_builder::select_clause::SelectClause<information_schema::information_schema::key_column_usage::columns::column_name>, diesel::query_builder::distinct_clause::NoDistinctClause, diesel::query_builder::where_clause::WhereClause<diesel::expression::operators::And<diesel::expression::operators::And<diesel::expression::array_comparison::In<information_schema::information_schema::key_column_usage::columns::constraint_name, diesel::expression::array_comparison::Subselect<diesel::query_builder::SelectStatement<information_schema::information_schema::table_constraints::table, diesel::query_builder::select_clause::SelectClause<information_schema::information_schema::table_constraints::columns::constraint_name>, diesel::query_builder::distinct_clause::NoDistinctClause, diesel::query_builder::where_clause::WhereClause<diesel::expression::operators::Eq<information_schema::information_schema::table_constraints::columns::constraint_type, diesel::expression::bound::Bound<diesel::types::Text, &str>>>>, diesel::types::Text>>, diesel::expression::operators::Eq<information_schema::information_schema::key_column_usage::columns::table_name, diesel::expression::bound::Bound<diesel::types::Text, &std::string::String>>>, diesel::expression::operators::Eq<information_schema::information_schema::key_column_usage::columns::table_schema, diesel::expression::bound::Bound<diesel::types::Text, std::string::String>>>>, diesel::query_builder::order_clause::OrderClause<information_schema::information_schema::key_column_usage::columns::ordinal_position>>` in the current scope
   --> /home/adona/.cargo/registry/src/github.com-1ecc6299db9ec823/diesel_infer_schema-0.16.0/src/information_schema.rs:156:10
    |
156 |         .load(conn)
    |          ^^^^
    |
    = note: the method `load` exists but the following trait bounds were not satisfied:
....
@sgrif

This comment has been minimized.

Member

sgrif commented Nov 8, 2017

Make sure you also point diesel_codegen and diesel_infer_schema at master

@attdona

This comment has been minimized.

attdona commented Nov 8, 2017

Ok, pointing at master also diesel_codegen and diesel_infer_schema it works as expected:

Just in case could help someone this is my setup (../diesel directory is the root of diesel github clone):

[dependencies]
diesel = {path = "../diesel/diesel", features = ["postgres"]}
diesel_infer_schema = {path = "../diesel/diesel_infer_schema", features = ["postgres"]}
diesel_codegen = {path = "../diesel/diesel_codegen", features = ["postgres"]}
[replace]
"diesel:0.16.0" = {path = "../diesel/diesel"}
"diesel_infer_schema:0.16.0" = {path = "../diesel/diesel_infer_schema"}
"diesel_codegen:0.16.0" = {path = "../diesel/diesel_codegen"}


@sgrif Thanks for your suggestions!

@sgrif sgrif closed this Nov 8, 2017

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment