Where communities thrive


  • Join over 1.5M+ people
  • Join over 100K+ communities
  • Free without limits
  • Create your own community
People
Repo info
Activity
    Johann Hemmann
    @Urhengulas

    @Urhengulas I would probably just try to call Connection::begin_test_connection() for each connection on pool creation via the r2d2::CustomizeConnection trait.
    Generally speaking: I think a guide how to write tests involving diesel is certainly something that would be a welcome addition for our web page.

    @weiznich Thanks. That sounds like an idea. If I come up with a good pattern I might send a description PR for the website :)

    Andrew Wheeler(Genusis)
    @genusistimelord
    Never mind i managed to figure out the issue. It is due to the Table Schema not having the variables in the same order as the struct OR the struct missing variables that exist within the table schema. Would be nice if it could use the struct to make the connection and mappings instead of the schema.
    Georg Semmler
    @weiznich
    @genusistimelord Can you describe a bit more in detail what you exactly mean by "Would be nice if it could use the struct to make the connection and mappings instead of the schema."?
    Erlend Langseth
    @Ploppz
                p::plate
                    .select(m::measurement.select(count_star()).filter(m::plate_id.eq(p::id)))
                    .load::<i64>(&conn)
    is this supported? I get some errors https://bpa.st/YP3A
    2 replies
    btw this is only the start of a query, I would still inner join plate with other tables and select several more columns
    Andrew Wheeler(Genusis)
    @genusistimelord
    Well what i mean @weiznich is instead of having to use the schema like this example
    diesel::table! {
        use crate::gametypes;
        use diesel::sql_types::*;
        players (uid) {
            uid -> BigInt,
            name -> Text,
            address -> Text,
            pos -> gametypes::PosType,
            vital -> Array<Integer>,
            indeath -> Bool,
        }
    }
    
    #[derive(Debug, PartialEq, Queryable, Insertable, Associations, Identifiable)]
    #[table_name = "players"]
    #[primary_key(uid)]
    pub struct PGPlayerWithID {
        uid: i64,
        name: String,
        address: String,
        pos: Position,
        vital: Vec<i32>,
        indeath: bool,
    }
    
    let pgplayer = players::table.filter(players::uid.eq(user.accid)).first::<PGPlayerWithID>(conn)?;
    that you could either have it map the table Select based on the components in the structure and use the structure directly to do the SQL Query. like this.
    diesel::table! {
        use crate::gametypes;
        use diesel::sql_types::*;
        players (uid) {
            uid -> BigInt,
            name -> Text,
            address -> Text,
            pos -> gametypes::PosType,
            vital -> Array<Integer>,
            indeath -> Bool,
        }
    }
    
    #[derive(Debug, PartialEq, Queryable, Insertable, Associations, Identifiable)]
    #[table_name = "players"]
    #[primary_key(uid)]
    pub struct PGPlayerWithID {
        uid: i64,
        name: String,
    }
    
    let pgplayer = PGPlayerWithID::select.filter(players::uid.eq(user.accid)).first::<PGPlayerWithID>(conn)?;
    Andrew Wheeler(Genusis)
    @genusistimelord
    which would be similar to
    players::table.select((players.uid, players.name)).filter(players::uid.eq(user.accid)).first::<PGPlayerWithID>(conn)?;
    Georg Semmler
    @weiznich
    @genusistimelord How would you support more complex select clauses in your example? It's important to support queries that do not only "just" select some fields as part of the select clause, but have more complex expressions there. For example such queries as in the example above your current post. (There is #2367 which implements something like that, but that's nothing that's on top of my priority list.)
    Andrew Wheeler(Genusis)
    @genusistimelord
    Yeah i was mostly just offering my Idea's on this. The main thing would be to support a single Schema with additional structs that could return different amounts of data based on the struct compared to the schema. for more advanced i would still recommend using the schema directly. just thinking of this as writing less code for a large project.
    but its not important.
    Georg Semmler
    @weiznich
    That shouldn't mean that this wouldn't be something that can be useful for anyone. I'm just not sure if this should be really part of diesel itself, or should there be some kind of "high-level" crate that uses diesel internally and provides an interface that matches more classical ORM 's. If someone steps up to write such an crate I would definitively try to over some advice and help for implementation there.
    Andrew Wheeler(Genusis)
    @genusistimelord
    @weiznich I would step up but I lack experience in Rust macros. I find them very hard to use right now as i really don't fully understand them.
    Georg Semmler
    @weiznich
    I think you doesn't need that much macros for such a project. More likely that you need to define a whole lot of traits and write some generic code + maybe one or two "simple" derive macros (but they are really simple, just take some rust code and generate some rust code, that's not any more complex than writing normal rust code)
    I think the more complex part is to decide how the "public" API of such a crate should look like and how to avoid common ORM problems like N+1 queries in such an API.
    I think @pksunkara Is working on something like that as well, so maybe you could ask them on their approach and join forces there?
    Andrew Wheeler(Genusis)
    @genusistimelord
    the n+1 issue is very hard to avoid XD
    Georg Semmler
    @weiznich
    At least diesel provides some API's that allow you to write queries without running in such issues. (See diesel::associations for example :wink:)
    Andrew Wheeler(Genusis)
    @genusistimelord
    I think maybe for something like this rather that relying on a full blown query hierarchy it be better to just add a derive that builds a players::table.select((players.uid, players.name)) that the end user could simply just use the rest of regular diesel along with it
    so you could do
    PGPlayerWithID::select() and the rest would be regular diesel commands
    Georg Semmler
    @weiznich
    For that see #2367. As written before: It's not a priority for myself, but I would be happy to accept a complete PR there.
    Andrew Wheeler(Genusis)
    @genusistimelord
    @weiznich Looking over that PR and i don't see anything wrong with why it hasn't been pushed yet?
    Georg Semmler
    @weiznich
    Because the CI is failing and something seems to be wrong with some type mapping there. As written there: I do currently not have the time or capacity to work on that. So if someone figures out what's wrong I will try to find some time to do a final review.
    Andrew Wheeler(Genusis)
    @genusistimelord
    ok i can probably look into it and see.
    Pavan Kumar Sunkara
    @pksunkara

    I think maybe for something like this rather that relying on a full blown query hierarchy it be better to just add a derive that builds a players::table.select((players.uid, players.name)) that the end user could simply just use the rest of regular diesel along with it

    I have actually finished implementing support for the above in my ORM lib (on top of diesel) using rust macros. I am currently working on solving this issue for INSERT and UPDATE. And then will move on to associations (I have a vague idea on how to solve the n+1 problem)

    Andrew Wheeler(Genusis)
    @genusistimelord
    @pksunkara have you posted this anywhere yet?
    Pavan Kumar Sunkara
    @pksunkara
    It uses diesel master
    Andrew Wheeler(Genusis)
    @genusistimelord
    looks really clean.
    1 reply
    Shao Chenyang
    @tzsword
    how to re-export diesel macros in workspace crate?
    Shao Chenyang
    @tzsword
    I implemented paging in the utils workspace, so I want to export diesel in utils to avoid version conflicts.
    The current solution is to add diesel to the dependencies in each workspace used
    Georg Semmler
    @weiznich
    @tzsword Have you tried to put a #[macro_export] on your reexport?
    Lee Bousfield
    @PlasmaPower
    It seems like a bug that you can use [u8; 8] in a derive Queryable struct but not a derive Insertable struct (you get an error about it not implementing Expression for the latter). I've worked around it with a serialize_as wrapper for now. Should I file a github issue or is there an underlying cause here I haven't thought about?
    Georg Semmler
    @weiznich
    @PlasmaPower Can you provide the a complete reproducing example for that before opening an issue?
    Lee Bousfield
    @PlasmaPower
    With diesel features=["postgres"] :
    #[macro_use]
    extern crate diesel;
    
    table! {
        examples (id) {
            id -> Int4,
            bytes -> Bytea,
        }
    }
    
    // Compiles
    #[derive(Queryable)]
    struct Example {
        id: i32,
        bytes: [u8; 8],
    }
    
    // Doesn't compile
    #[derive(Insertable)]
    #[table_name = "examples"]
    struct NewExample {
        bytes: [u8; 8],
    }
    let me look at an example without postgres..
    Georg Semmler
    @weiznich
    Have you tried to actually use your struct implementing Queryable? That won't work.
    Lee Bousfield
    @PlasmaPower
    oh, shoot. That's too bad. I guess I'll need a wrapper on both sides then.
    thanks
    Georg Semmler
    @weiznich
    The "underlying" issue is that we cannot guarantee that the returned Bytea field is exactly 8 byte. It also can contain more than 8 bytes for the general case.
    Lee Bousfield
    @PlasmaPower
    would this work with a mysql engine BYTES(8)?
    Georg Semmler
    @weiznich
    As diesel only has a general Bytea type this won't help directly. You would probably need to define your own sql type for that and then your definitively back to writing your own conversions there.
    Lee Bousfield
    @PlasmaPower
    alright, thanks
    Shao Chenyang
    @tzsword

    @tzsword Have you tried to put a #[macro_export] on your reexport?

    thank you very very very much for reply. I gave up, I added diesel to the required workspace, and gave up paging hahaha~~

    for now, my code looks like:

           table
          .filter(available.eq(query.available()))
                .limit(query.limit())
                .offset(query.offset());

    diesel is AWESOME~~~

    Georg Semmler
    @weiznich
    :+1:
    Pavan Kumar Sunkara
    @pksunkara
    @tzsword I have actually been looking into this issue last week. The issue is that diesel uses some derive macros inside it non proc macros which is why re-exporting doesn't work. I am planning to solve it soon.
    Pavan Kumar Sunkara
    @pksunkara
            #[derive(Debug, Clone, Copy, $crate::query_builder::QueryId, Default)]
            pub struct $column_name;
    is the offender
    Miguel Ping
    @mping
    Hi