Where communities thrive


  • Join over 1.5M+ people
  • Join over 100K+ communities
  • Free without limits
  • Create your own community
People
Repo info
Activity
    sprhawk
    @sprhawk
    Hi, I got an example here
    Georg Semmler
    @weiznich
    sprhawk
    @sprhawk
    I tried like this Select<LeftJoinAB, (a::SqlType, Nullable<b::SqlType>)>, error shows
    10 error[E0277]: the trait bound `diesel::query_builder::SelectStatement<JoinOn<diesel::query_source::joins::Join<a::table, b::table, LeftOuter>, diesel::expression::operators::Eq<diesel::expression::nullable::Nullable<b::columns::a_id>, diesel::expression::nullable::Nullable<a::columns::id>>>>: SelectDsl<((diesel:\
        :sql_types::Nullable<diesel::sql_types::Integer>, diesel::sql_types::Nullable<diesel::sql_types::Integer>), diesel::expression::nullable::Nullable<(diesel::sql_types::Nullable<diesel::sql_types::Integer>, diesel::sql_types::Nullable<diesel::sql_types::Integer>)>)>` is not satisfied                                
     11   --> src/main.rs:22:1                                                                                                                                                                                                                                                                                                    
     12    |                                                                                                                                                                                                                                                                                                                      
     13 22 | / fn query_ab() -> Select<LeftJoinAB, (a::SqlType, Nullable<b::SqlType>)> {                                                                                                                                                                                                                                          
     14 23 | |     let join = a_left_join_b();                                                                                                                                                                                                                                                                                    
     15 24 | |     join.select((a::all_columns, b::all_columns.nullable()))                                                                                                                                                                                                                                                       
     16 25 | | }                                                                                                                                                                                                                                                                                                                  
     17    | |_^ the trait `SelectDsl<((diesel::sql_types::Nullable<diesel::sql_types::Integer>, diesel::sql_types::Nullable<diesel::sql_types::Integer>), diesel::expression::nullable::Nullable<(diesel::sql_types::Nullable<diesel::sql_types::Integer>, diesel::sql_types::Nullable<diesel::sql_types::Integer>)>)>` is not i\
        mplemented for `diesel::query_builder::SelectStatement<JoinOn<diesel::query_source::joins::Join<a::table, b::table, LeftOuter>, diesel::expression::operators::Eq<diesel::expression::nullable::Nullable<b::columns::a_id>, diesel::expression::nullable::Nullable<a::columns::id>>>>`
    I think the second argument of Select should be all columns? I see the source code the table::SqlType is defined as types of all columns, so I used it.
    Georg Semmler
    @weiznich
    The second argument is not the SqlType of the selection, but the type of the selection itself. So something like (a::AllColumns, Nullable<b::AllColumns>).
    thecooldrop
    @thecooldrop

    Hello everyone, I am trying to implement optimistic locking generically over all structs which have some specific properties. Reason I want to do this, is because all of my structs are going to be optimistically locked, and I do not want to repeat the read-check-update code many times. The idea is that optimistic locking is basically just read and update. I have started writing the code, but generality of Diesel has left me awe-struck. Up to now I have following bit of code, and following error:

    pub trait Versionable {
        type Output: Identifiable;
        fn update(&self, conn: &PgConnection) -> Self;
    }
    
    impl<T> Versionable for T where
        T: Identifiable + HasTable
    {
        type Output = T;
    
        fn update(&self, conn: &PgConnection) -> Self {
            conn.transaction(|| {
                let table = Self::table();
                let pk = table.primary_key();
                let row = table.filter(table, pk.eq(&self.id()));
                unimplemented!()
            });
            unimplemented!()
        }
    }

    and error resulting is

    error[E0275]: overflow evaluating the requirement `_: Sized`
      --> src/product/category/models.rs:62:29
       |
    62 |             let row = table.filter(table, pk.eq(&self.id()));
       |                             ^^^^^^
       |
       = help: consider adding a `#![recursion_limit="4096"]` attribute to your crate (`warehouse`)
       = note: required because of the requirements on the impl of `FilterDsl<_>` for `<<T as HasTable>::Table as diesel::query_builder::AsQuery>::Query`

    It seems like the content of <<T as HasTable>::Table as diesel::query_builder::AsQuery>::Query may again be a type implementing trait Table, which leads to infinite recursion. The path by which this seems to be possible is QueryDsl::filter -> FilterDsl<Predicate>::filter -> QueryDsl::filter .....

    My first question is how do I resolve this? How do I say that <<T as HasTable>::Table as diesel::query_builder::AsQuery>::Query is actually something which does not loop back to QueryDsl via FilteringDsl<Predicate>?

    Second question I have is that I have noticed, that I have no constraint that primary key, here variable pk is comparable to &self.id(). How would one express that using Diesel traits?

    9 replies
    luopan
    @loloxiaoz

    I'm calling a custom sql function which takes several arrays of composite types as arguments and getting DatabaseError(__Unknown, "wrong element type"). I've tried the debug_query function but it seems to be showing me the Rust representation of the bind parameters, and I would like to see how the parameters are being sent over the wire; is there a way to do this?

    I had encounter the same question, is there a solution?

    2 replies
    image.png
    image.png
    I wan't to insert array<Inetrange> into postgres with diesel
    but i got a error "saving new ipaddress: DatabaseError(__Unknown, "wrong element type"
    singhjass
    @singhjass

    select Products.ProductName as ProductName,OrderItems.Quantity,OrderItems.Height,OrderItems.Width,CONVERT(varchar(100), OrderItems.TotalPrice) as [TotalPrice] from Orders
    inner join OrderItems
    on Orders.Id=OrderItems.OrderId
    join Products on Products.ProductId=OrderItems.ProductId
    where Orders.Id=@orderid

    Union all

    select '- ' + Products.ProductName as ProductName,null,null,null,null from Orders
    inner join OrderSubItems join OrderItems
    on OrderItems.Id = OrderSubItems.OrderItemId
    on Orders.Id=OrderItems.OrderId
    join Products on Products.ProductId = OrderSubItems.ProductId
    where Orders.Id=@orderid

    Does nyone know how to append main category first row with subcategory
    1--->1,1,1::2--->2,2,2
    right now the output is 111222
    Georg Semmler
    @weiznich
    Please format your messages in such a way that they are readable. Also you need to include at least some information about the used database system and such things.
    singhjass
    @singhjass
    Its sql
    Georg Semmler
    @weiznich
    It's up to you to provide the necessary information. Currently it's not possible to answer this question.
    singhjass
    @singhjass
    i need to append main row 1st and then its sub categories and similarly second row with its sub categories
    but right now its coming like main category(allitems)+subcategory(allitems)
    Georg Semmler
    @weiznich
    Again: If you look for answers here please format your message in such a way that it is readable and provide the necessary information like which database system do you use, what exactly do you want to know and how is this related to diesel. Otherwise this is my last comment on your question.
    thecooldrop
    @thecooldrop
    Hi @weiznich , I hope that my question was up to standards :D
    2 replies
    Hoang Tran
    @hoangtranwork

    hi, I'm trying to construct this where clause with nested and/or:

    SELECT *
    FROM `pieces_users`
    WHERE `user_id` = ?
      AND (
            (`itemType` = ? AND `bookId` > ?)
            OR
            (`itemType` = ? AND `bundleId` > ?)
        );

    Here's what I tried:

            let query = pieces_users
                .filter(
                    pu_user_id.eq(user_id_).and(
                        (pu_item_type.eq(1).and(pu_book_id.gt(0)))
                            .or(pu_item_type.eq(0).and(pu_bundle_id.gt(0))),
                    ),
                )
                .select(pieces_users::all_columns())

    However the generated SQL WHERE is flattened:

    SELECT *
    FROM `pieces_users`
    WHERE `user_id` = ?
      AND (`itemType` = ? AND `bookId` > ? OR `itemType` = ? AND `bundleId` > ?)

    How can I control the nested AND/OR construction? I've read this https://docs.diesel.rs/master/diesel/expression_methods/trait.BoolExpressionMethods.html#method.and but it only contains simpler cases

    5 replies
    HarmoGlace
    @zaitara:matrix.org
    [m]
    Why does intellij do not find my variable internal ?
    Georg Semmler
    @weiznich
    @zaitara:matrix.org Can you give a bit context how this is related to diesel and so on?
    HarmoGlace
    @zaitara:matrix.org
    [m]
    I was just asking if diesel is compatible with intellij
    Georg Semmler
    @weiznich
    That depends on what you mean by compatible. Diesel does nothing per se to be not compatible with any IDE. If you encounter issues that's likely something you should report to the corresponding IDE and not to diesel.
    mang0
    @dea223:matrix.org
    [m]
    hi there
    chaf16
    @chaf16:matrix.org
    [m]
    Hi
    HarmoGlace
    @zaitara:matrix.org
    [m]
    Is it possible to define table without the table macro ?
    Georg Semmler
    @weiznich
    @zaitara:matrix.org Technically speaking it is possible to write all the generated code by yourself, but that would not be very practical as this would mean that you have to write a few hundred lines of code per table.
    HarmoGlace
    @zaitara:matrix.org
    [m]
    I see, but my IDE does not support diesel macro, do you know one which does support it?
    Georg Semmler
    @weiznich
    I think rust-analyzer is supporting it quite well. Otherwise I do consider that a problem of your IDE, not of diesel as we write code that is accepted by rustc not by a specific IDE.
    hadis
    @hadis:matrix.org
    [m]
    Is it possible to write a generic load by id function somewhat like this:
    fn load<M>(id: M::Id, conn: &PgConnection) -> QueryResult<M> { ... }

    This is what I currently have:

    fn load<M>(&self, id: M::Id, connection: &PgConnection) ->QueryResult<M>
    where
        M: Identifiable,
        Find<dyn FindDsl<M::Id, Output = M>, M::Id>: LoadQuery<PgConnection, M>,
    {
        M::table().find(id).get_result(connection)

    It doesn't compile:

    error[E0275]: overflow evaluating the requirement `_: Sized`
    --> src/bin/server/database.rs:179:16
    |
    179 |     I::table().find(id).get_result(conn)
    |                ^^^^
    |
    = help: consider adding a `#![recursion_limit="4096"]` attribute to your crate (`server`)
    = note: required because of the requirements on the impl of `FilterDsl<_>` for `<<I as HasTable>::Table as AsQuery>::Query`
    
    error: aborting due to previous error; 6 warnings emitted
    1 reply
    It does compile if I pass the table as a parameter but that's not what I want.
    HarmoGlace
    @zaitara:matrix.org
    [m]
    I do not understand how to use PgNumeric. For instance, I want to convert 534.12 into a pgnumeric, but it doesn't work
    1 reply
    bcba25
    @bcba25

    Hello everyone. I got a question on raw queries. Haven’t found much on diesel’s documentation.

    const QUERY: &str = r#"SELECT "id", "user_id", "name", "location", ST_Distance(...) as "distance" FROM profile WHERE ST_Distance(...) <= 1000 ORDER BY "distance" ASC"#; let profiles = diesel::sql_query(QUERY).load::<ProfileNearby>(_connection);

    I’m wondering how can I use my custom structs with diesel and raw queries. If I use load::<Profile> everything is ok and it builds fine, the only problem is that result doesn’t have distance column as it is not specified on a struct and doesn’t exists on table. Do you know how I can bind a custom struct to query results ?

    HarmoGlace
    @zaitara:matrix.org
    [m]
    Well it seems that I did not have the good version. Thank you!
    HarmoGlace
    @zaitara:matrix.org
    [m]
    I would like to compare an integer but can't find it on the doc, and the eq methods seems only to work on boolean ? How can I check if an integer is equal to another integer ?
    Georg Semmler
    @weiznich
    You want to use QueryDsl::filter instead. find is only for searching using a primary key.
    HarmoGlace
    @zaitara:matrix.org
    [m]
    I see, thank you
    HarmoGlace
    @zaitara:matrix.org
    [m]
    Does diesel support postgres composite type?
    Georg Semmler
    @weiznich
    @zaitara:matrix.org Yes. See the documentation for the record type for details.
    Boscop
    @Boscop
    It seems sqlite-bundled only applies to diesel_cli. So which sqlite dll does the diesel library call? (At which path)
    Georg Semmler
    @weiznich
    @Boscop Diesel calls whichever sqlite dll the linker provides at runtime. That is something that really depends on your environment.
    Boscop
    @Boscop
    @weiznich Ah right, I found it again.. Does it need a specific version of sqlite? Or can I always use the latest release's dll?