Coder Social home page Coder Social logo

Comments (8)

sgrif avatar sgrif commented on May 22, 2024

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

from diesel.

defyrlt avatar defyrlt commented on May 22, 2024

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`.

from diesel.

sgrif avatar sgrif commented on May 22, 2024

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

from diesel.

defyrlt avatar defyrlt commented on May 22, 2024

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`.

from diesel.

sgrif avatar sgrif commented on May 22, 2024

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

from diesel.

defyrlt avatar defyrlt commented on May 22, 2024

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?

from diesel.

sgrif avatar sgrif commented on May 22, 2024

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.

from diesel.

defyrlt avatar defyrlt commented on May 22, 2024

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?

from diesel.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.