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::debug_query doesn't show the RETURNING part of query when used with PostgreSQL #1249

Closed
blazern opened this Issue Oct 9, 2017 · 3 comments

Comments

Projects
None yet
2 participants
@blazern

blazern commented Oct 9, 2017

Setup

Versions

  • Rust: rustc 1.19.0 (0ade33941 2017-07-17)
  • Diesel: 0.16.0
  • Database: PostgreSQL 9.5.8
  • Operating System Ubuntu 16.04

Feature Flags

  • diesel: postgres, uuid
  • diesel_codegen: postgres

Problem Description

Created an PostgreSQL table and a user, the user was granted only with INSERT privilege.
Used infer_schema to generate wrappers and correct URL to connect to the local DB.
Executed:

    diesel::insert(&my_inserted_value).into(schema::my_table_name::table).get_result(&connection)

which failed with DieselError(DatabaseError(__Unknown, "permission denied for relation my_table_name").

Tried to insert the same value manually by psql - the insertion worked.
Decided to use diesel::debug_query to see what query Diesel used to INSERT.

Executed:

    let query = diesel::insert(&my_inserted_value).into(schema::my_table_name::table);
    let debug = diesel::debug_query::<diesel::pg::Pg, _>(&query);
    println!("The insert query: {:?}", debug);

Which printed out:

The insert query: Query { sql: "INSERT INTO \"my_table_name\" (\"uid\", \"k_uid\") VALUES ($1, $2)", binds: [Uuid("b351f809-3e4c-40cd-bb64-d8c8c07c1086"), 123] }

Tried to execute this very query manually by psql - manual insertion worked, while insertion by Diesel failed.

Opened PostgreSQL logs, and there was the real query which Diesel used:

db_client@db_main STATEMENT:  INSERT INTO "my_table_name" ("uid", "k_uid") VALUES ($1, $2) RETURNING "my_table_name"."id", "my_table_name"."uid", "my_table_name"."k_uid"

The RETURNING clause was the problem - it requires SELECT privilege, but the user I used had only INSERT privilege.

If diesel::debug_query shown the correct query I wouln't have to look it up in the logs.

Checklist

  • I have already looked over the issue tracker for similar issues.
@sgrif

This comment has been minimized.

Member

sgrif commented Oct 9, 2017

There's not really anything we can do here. debug_query shows the SQL of the query as it exists at that point (as if it were passed to execute). If we tried to mirror the behavior of get_results here, it would cause debug_query to stop working on all backends other than PG. You can append the returning clause yourself by calling .as_query or .returning(table::all_columns)

@sgrif sgrif closed this Oct 9, 2017

@blazern

This comment has been minimized.

blazern commented Oct 10, 2017

@sgrif, is there a way to see the full query used by Diesel though?
If one can't see the full query used by Diesel, it can be hard to debug wrong behaviour.

@sgrif

This comment has been minimized.

Member

sgrif commented Oct 13, 2017

My last comment described how to see the full query, depending on whether it is run with execute or get_results

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