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

Example from README is not working #36

Closed
defyrlt opened this Issue Nov 30, 2015 · 8 comments

Comments

Projects
None yet
2 participants
@defyrlt

defyrlt commented Nov 30, 2015

I'm trying this example from the README (added some imports from previous one with table! macro):

#![feature(custom_derive)]

#[macro_use]
extern crate diesel;

use diesel::*;

#[derive(Queriable, Debug)]
pub struct User {
    id: i32,
    name: String,
    favorite_color: Option<String>,
}

fn main() {
    let connection = Connection::establish(env!("DATABASE_URL"))
        .unwrap();
    let users: Vec<User> = users::table.load(&connection)
        .unwrap().collect();

    println!("Here are all the users in our database: {:?}", users);
}

Here's my error msg:

   Compiling try_diesel v0.1.0 (file:///home/user/rust/try_diesel)
src/main.rs:18:28: 18:40 error: failed to resolve. Use of undeclared type or module `users` [E0433]
src/main.rs:18     let users: Vec<User> = users::table.load(&connection)
                                          ^~~~~~~~~~~~
src/main.rs:18:28: 18:40 help: run `rustc --explain E0433` to see a detailed explanation
src/main.rs:18:28: 18:40 error: unresolved name `users::table` [E0425]
src/main.rs:18     let users: Vec<User> = users::table.load(&connection)
                                          ^~~~~~~~~~~~
src/main.rs:18:28: 18:40 help: run `rustc --explain E0425` to see a detailed explanation
error: aborting due to 2 previous errors
Could not compile `try_diesel`.

rustc:

rustc 1.6.0-nightly (b7845f93b 2015-11-15)
binary: rustc
commit-hash: b7845f93b54d3e45fcac94e7d7f3111aad90142f
commit-date: 2015-11-15
host: x86_64-unknown-linux-gnu
release: 1.6.0-nightly
@sgrif

This comment has been minimized.

Member

sgrif commented Nov 30, 2015

Are you useung the table from the other module? I don't see it in your code. Can you post the full code?

@defyrlt

This comment has been minimized.

defyrlt commented Nov 30, 2015

That's the full code. I thought that deriveing Queriable will be enough.
Using table! macro does not make it compile though.

#![feature(custom_derive)]

#[macro_use]
extern crate diesel;

use diesel::*;

table! {
    users {
        id -> Serial,
        name -> VarChar,
        favorite_color -> Nullable<VarChar>,
    }
}

#[derive(Queriable, Debug)]
pub struct User {
    id: i32,
    name: String,
    favorite_color: Option<String>,
}

fn main() {
    let connection = Connection::establish(env!("DATABASE_URL"))
        .unwrap();
    let users: Vec<User> = users::table.load(&connection)
        .unwrap().collect();

    println!("Here are all the users in our database: {:?}", users);
}
src/main.rs:27:19: 27:28 error: the trait `diesel::query_source::Queriable<(diesel::types::Integer, diesel::types::VarChar, diesel::types::Nullable<diesel::types::VarChar>)>` is not implemented for the type `User` [E0277]
src/main.rs:27         .unwrap().collect();
                                 ^~~~~~~~~
src/main.rs:27:19: 27:28 help: run `rustc --explain E0277` to see a detailed explanation
src/main.rs:26:41: 26:58 error: the trait `diesel::query_source::Queriable<(diesel::types::Integer, diesel::types::VarChar, diesel::types::Nullable<diesel::types::VarChar>)>` is not implemented for the type `User` [E0277]
src/main.rs:26     let users: Vec<User> = users::table.load(&connection)
                                                       ^~~~~~~~~~~~~~~~~
src/main.rs:26:41: 26:58 help: run `rustc --explain E0277` to see a detailed explanation
error: aborting due to 2 previous errors
Could not compile `try_diesel`.
@sgrif

This comment has been minimized.

Member

sgrif commented Nov 30, 2015

Deriving should be enough. Have you followed the instructions at https://github.com/sgrif/diesel/blob/master/diesel_codegen/README.md#using-on-nightly? This looks like you're probably just missing the default-features = false, features = ["nightly"] on codegen

@defyrlt

This comment has been minimized.

defyrlt commented Nov 30, 2015

If I don't use table! - where should I get the users module?
Naa, it didn't work out :(

[dependencies]
diesel = "*"
diesel_codegen = { version = "^0.1.0", default-features = false, features = ["nightly"] }
#![feature(custom_derive, custom_attribute, plugin)]
#![plugin(diesel_codegen)]

#[macro_use]
extern crate diesel;
extern crate diesel_codegen;

use diesel::*;
   Compiling aster v0.8.0
/home/user/.cargo/registry/src/github.com-0a35038f75765ae4/aster-0.8.0/src/item.rs:950:35: 950:52 error: use of undeclared type name `ast::ImplItemKind` [E0412]
/home/user/.cargo/registry/src/github.com-0a35038f75765ae4/aster-0.8.0/src/item.rs:950     pub fn build_item(self, node: ast::ImplItemKind) -> F::Result {
                                                                                                                         ^~~~~~~~~~~~~~~~~
/home/user/.cargo/registry/src/github.com-0a35038f75765ae4/aster-0.8.0/src/item.rs:950:35: 950:52 help: run `rustc --explain E0412` to see a detailed explanation
/home/user/.cargo/registry/src/github.com-0a35038f75765ae4/aster-0.8.0/src/item.rs:979:20: 979:44 error: failed to resolve. Could not find `ImplItemKind` in `syntax::ast` [E0433]
/home/user/.cargo/registry/src/github.com-0a35038f75765ae4/aster-0.8.0/src/item.rs:979         let node = ast::ImplItemKind::Const(const_.ty, const_.expr.expect("an expr is required for a const impl item"));
                                                                                                          ^~~~~~~~~~~~~~~~~~~~~~~~
/home/user/.cargo/registry/src/github.com-0a35038f75765ae4/aster-0.8.0/src/item.rs:979:20: 979:44 help: run `rustc --explain E0433` to see a detailed explanation
/home/user/.cargo/registry/src/github.com-0a35038f75765ae4/aster-0.8.0/src/item.rs:979:20: 979:44 error: unresolved name `ast::ImplItemKind::Const` [E0425]
/home/user/.cargo/registry/src/github.com-0a35038f75765ae4/aster-0.8.0/src/item.rs:979         let node = ast::ImplItemKind::Const(const_.ty, const_.expr.expect("an expr is required for a const impl item"));
                                                                                                          ^~~~~~~~~~~~~~~~~~~~~~~~
/home/user/.cargo/registry/src/github.com-0a35038f75765ae4/aster-0.8.0/src/item.rs:979:20: 979:44 help: run `rustc --explain E0425` to see a detailed explanation
/home/user/.cargo/registry/src/github.com-0a35038f75765ae4/aster-0.8.0/src/item.rs:990:20: 990:45 error: failed to resolve. Could not find `ImplItemKind` in `syntax::ast` [E0433]
/home/user/.cargo/registry/src/github.com-0a35038f75765ae4/aster-0.8.0/src/item.rs:990         let node = ast::ImplItemKind::Method(method.sig, method.block.expect("a block is required for a method impl item"));
                                                                                                          ^~~~~~~~~~~~~~~~~~~~~~~~~
/home/user/.cargo/registry/src/github.com-0a35038f75765ae4/aster-0.8.0/src/item.rs:990:20: 990:45 help: run `rustc --explain E0433` to see a detailed explanation
/home/user/.cargo/registry/src/github.com-0a35038f75765ae4/aster-0.8.0/src/item.rs:990:20: 990:45 error: unresolved name `ast::ImplItemKind::Method` [E0425]
/home/user/.cargo/registry/src/github.com-0a35038f75765ae4/aster-0.8.0/src/item.rs:990         let node = ast::ImplItemKind::Method(method.sig, method.block.expect("a block is required for a method impl item"));
                                                                                                          ^~~~~~~~~~~~~~~~~~~~~~~~~
/home/user/.cargo/registry/src/github.com-0a35038f75765ae4/aster-0.8.0/src/item.rs:990:20: 990:45 help: run `rustc --explain E0425` to see a detailed explanation
/home/user/.cargo/registry/src/github.com-0a35038f75765ae4/aster-0.8.0/src/item.rs:1001:20: 1001:43 error: failed to resolve. Could not find `ImplItemKind` in `syntax::ast` [E0433]
/home/user/.cargo/registry/src/github.com-0a35038f75765ae4/aster-0.8.0/src/item.rs:1001         let node = ast::ImplItemKind::Type(ty);
                                                                                                           ^~~~~~~~~~~~~~~~~~~~~~~
/home/user/.cargo/registry/src/github.com-0a35038f75765ae4/aster-0.8.0/src/item.rs:1001:20: 1001:43 help: run `rustc --explain E0433` to see a detailed explanation
/home/user/.cargo/registry/src/github.com-0a35038f75765ae4/aster-0.8.0/src/item.rs:1001:20: 1001:43 error: unresolved name `ast::ImplItemKind::Type` [E0425]
/home/user/.cargo/registry/src/github.com-0a35038f75765ae4/aster-0.8.0/src/item.rs:1001         let node = ast::ImplItemKind::Type(ty);
                                                                                                           ^~~~~~~~~~~~~~~~~~~~~~~
/home/user/.cargo/registry/src/github.com-0a35038f75765ae4/aster-0.8.0/src/item.rs:1001:20: 1001:43 help: run `rustc --explain E0425` to see a detailed explanation
/home/user/.cargo/registry/src/github.com-0a35038f75765ae4/aster-0.8.0/src/item.rs:1012:20: 1012:44 error: failed to resolve. Could not find `ImplItemKind` in `syntax::ast` [E0433]
/home/user/.cargo/registry/src/github.com-0a35038f75765ae4/aster-0.8.0/src/item.rs:1012         let node = ast::ImplItemKind::Macro(mac);
                                                                                                           ^~~~~~~~~~~~~~~~~~~~~~~~
/home/user/.cargo/registry/src/github.com-0a35038f75765ae4/aster-0.8.0/src/item.rs:1012:20: 1012:44 help: run `rustc --explain E0433` to see a detailed explanation
/home/user/.cargo/registry/src/github.com-0a35038f75765ae4/aster-0.8.0/src/item.rs:1012:20: 1012:44 error: unresolved name `ast::ImplItemKind::Macro` [E0425]
/home/user/.cargo/registry/src/github.com-0a35038f75765ae4/aster-0.8.0/src/item.rs:1012         let node = ast::ImplItemKind::Macro(mac);
                                                                                                           ^~~~~~~~~~~~~~~~~~~~~~~~
/home/user/.cargo/registry/src/github.com-0a35038f75765ae4/aster-0.8.0/src/item.rs:1012:20: 1012:44 help: run `rustc --explain E0425` to see a detailed explanation
error: aborting due to 9 previous errors
Could not compile `aster`.

And I make it diesel_codegen = "*"

src/main.rs:29:19: 29:28 error: the trait `diesel::query_source::Queriable<(diesel::types::Integer, diesel::types::VarChar, diesel::types::Nullable<diesel::types::VarChar>)>` is not implemented for the type `User` [E0277]
src/main.rs:29         .unwrap().collect();
                                 ^~~~~~~~~
src/main.rs:29:19: 29:28 help: run `rustc --explain E0277` to see a detailed explanation
src/main.rs:28:41: 28:58 error: the trait `diesel::query_source::Queriable<(diesel::types::Integer, diesel::types::VarChar, diesel::types::Nullable<diesel::types::VarChar>)>` is not implemented for the type `User` [E0277]
src/main.rs:28     let users: Vec<User> = users::table.load(&connection)
                                                       ^~~~~~~~~~~~~~~~~
src/main.rs:28:41: 28:58 help: run `rustc --explain E0277` to see a detailed explanation
error: aborting due to 2 previous errors
Could not compile `try_diesel`.
@sgrif

This comment has been minimized.

Member

sgrif commented Nov 30, 2015

I should probably make it more clear in the README, table! must be called, we don't derive it for you. As for the first set of errors, try updating to the latest nightly, or use Syntex + stable

@defyrlt

This comment has been minimized.

defyrlt commented Nov 30, 2015

Got latest nightly. It works! Sorry, I should've done this before.
How about making #[derive(Queriable)] smart enough to produce appropriate table! call? Does it seem to be possible?

@sgrif

This comment has been minimized.

Member

sgrif commented Nov 30, 2015

The problem is that the type of your struct can very much not be one-to-one with the types of the table. I am planning on eventually adding load_table_from_schema!(table_name) and load_schema!, which will use a database connection at compile time to get this information from the database.

@defyrlt

This comment has been minimized.

defyrlt commented Nov 30, 2015

How about marking fields like this?

#[derive(Queriable, Debug)]
pub struct User {
    #[column_type = Serial]
    id: i32,
    #[column_type = VarChar]
    name: String,
}

UPD: this particular issue is closed. Let's make another one for automatic generation of table! call?

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