-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add a flag to help users better debug bad
Queryable
impls
Since the impl we generate is so generic, when a bad `Queryable` impl is given, the error doesn't occur until they actually try to load a query. This ends up being really non-local, and gives little to no feedback about which field is actually the problem. There is a new option called `#[check_types]` which completely changes how the impl is generated, in such a way that when the `unstable` feature is enabled the error will point at the specific field that is the problem. The actual message is the same, but where it points makes debugging much easier. This flag also generates code which validates that your fields are in the right order to match your columns. Unfortunately, we cannot have this generate an actual usable impl. Trait bounds not matching in types or where clauses will always result in an error pointing at the derive itself, which is no more helpful than where we started. For that reason, we can't actually have any usable `Row` type. So this flag is for debugging purposes only. This does not help with cases where the number of fields is incorrect.
- Loading branch information
Showing
9 changed files
with
299 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
37 changes: 37 additions & 0 deletions
37
diesel_compile_tests/tests/ui/derive_queryable_checked_sql_type.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
#[macro_use] | ||
extern crate diesel; | ||
|
||
use diesel::sql_types::{Nullable, Text}; | ||
use diesel::pg::Pg; | ||
|
||
table! { | ||
users { | ||
id -> Integer, | ||
} | ||
} | ||
|
||
#[derive(Queryable)] | ||
#[table_name = "users"] | ||
#[check_types(backend = "Pg")] | ||
struct User { | ||
id: String, | ||
#[sql_type = "Nullable<Text>"] | ||
name: String, | ||
} | ||
|
||
table! { | ||
posts { | ||
id -> Integer, | ||
user_id -> Integer, | ||
} | ||
} | ||
|
||
#[derive(Queryable)] | ||
#[table_name = "posts"] | ||
#[check_types(backend = "Pg")] | ||
struct Post { | ||
user_id: i32, | ||
id: i32, | ||
} | ||
|
||
fn main() {} |
54 changes: 54 additions & 0 deletions
54
diesel_compile_tests/tests/ui/derive_queryable_checked_sql_type.stderr
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
error[E0277]: the trait bound `*const str: diesel::deserialize::FromSql<diesel::sql_types::Integer, diesel::pg::Pg>` is not satisfied | ||
--> $DIR/derive_queryable_checked_sql_type.rs:17:5 | ||
| | ||
17 | id: String, | ||
| ^^ the trait `diesel::deserialize::FromSql<diesel::sql_types::Integer, diesel::pg::Pg>` is not implemented for `*const str` | ||
| | ||
= help: the following implementations were found: | ||
<*const str as diesel::deserialize::FromSql<diesel::sql_types::Text, diesel::sqlite::Sqlite>> | ||
<*const str as diesel::deserialize::FromSql<diesel::sql_types::Text, DB>> | ||
<*const str as diesel::deserialize::FromSql<diesel::sql_types::Time, diesel::sqlite::Sqlite>> | ||
<*const [u8] as diesel::deserialize::FromSql<diesel::sql_types::Binary, diesel::sqlite::Sqlite>> | ||
and 3 others | ||
= note: required because of the requirements on the impl of `diesel::deserialize::FromSql<diesel::sql_types::Integer, diesel::pg::Pg>` for `std::string::String` | ||
= note: required because of the requirements on the impl of `diesel::Queryable<diesel::sql_types::Integer, diesel::pg::Pg>` for `std::string::String` | ||
|
||
error[E0277]: the trait bound `*const str: diesel::deserialize::FromSql<diesel::sql_types::Nullable<diesel::sql_types::Text>, diesel::pg::Pg>` is not satisfied | ||
--> $DIR/derive_queryable_checked_sql_type.rs:18:5 | ||
| | ||
18 | #[sql_type = "Nullable<Text>"] | ||
| ^ the trait `diesel::deserialize::FromSql<diesel::sql_types::Nullable<diesel::sql_types::Text>, diesel::pg::Pg>` is not implemented for `*const str` | ||
| | ||
= help: the following implementations were found: | ||
<*const str as diesel::deserialize::FromSql<diesel::sql_types::Text, diesel::sqlite::Sqlite>> | ||
<*const str as diesel::deserialize::FromSql<diesel::sql_types::Text, DB>> | ||
<*const str as diesel::deserialize::FromSql<diesel::sql_types::Time, diesel::sqlite::Sqlite>> | ||
<*const [u8] as diesel::deserialize::FromSql<diesel::sql_types::Binary, diesel::sqlite::Sqlite>> | ||
and 3 others | ||
= note: required because of the requirements on the impl of `diesel::deserialize::FromSql<diesel::sql_types::Nullable<diesel::sql_types::Text>, diesel::pg::Pg>` for `std::string::String` | ||
= note: required because of the requirements on the impl of `diesel::Queryable<diesel::sql_types::Nullable<diesel::sql_types::Text>, diesel::pg::Pg>` for `std::string::String` | ||
|
||
error[E0308]: mismatched types | ||
--> $DIR/derive_queryable_checked_sql_type.rs:33:5 | ||
| | ||
1 | | #[macro_use] | ||
| |_^ expected struct `posts::columns::id`, found struct `posts::columns::user_id` | ||
... | ||
33| / user_id: i32, | ||
| | ||
= note: expected type `posts::columns::id` | ||
found type `posts::columns::user_id` | ||
|
||
error[E0308]: mismatched types | ||
--> $DIR/derive_queryable_checked_sql_type.rs:34:5 | ||
| | ||
1 | | #[macro_use] | ||
| |_^ expected struct `posts::columns::user_id`, found struct `posts::columns::id` | ||
... | ||
34| / id: i32, | ||
| | ||
= note: expected type `posts::columns::user_id` | ||
found type `posts::columns::id` | ||
|
||
error: aborting due to 4 previous errors | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.