Where communities thrive


  • Join over 1.5M+ people
  • Join over 100K+ communities
  • Free without limits
  • Create your own community
People
Repo info
Activity
    Georg Semmler
    @weiznich
    You can combine that, your select clause just needs to have the same shape as your load types. For your case that means something like .select(((table_1_column1, table_1_column2), (table_2_column1, table_2_column2)).
    sprhawk
    @sprhawk
    @weiznich Thanks, I'll try it
    sprhawk
    @sprhawk
    @weiznich hi, but can I combine usage of select((table_1_column, ...), (table_2::all_columns, ))? i found compiler error
    Georg Semmler
    @weiznich
    It's hard to tell you what's wrong without knowing the exact code and the exact error message. With a generic example I can only tell you that it should work that way.
    sprhawk
    @sprhawk
    @weiznich Oh, I found I added .nullable to second all_columns, and Option to second Queryable Struct, it compiles
    Georg Semmler
    @weiznich
    @/all As there is a request from the compiler team to collect non-optimal error messages emitted by rustc, I've put together a small repository with some error occurring by (mis-)using diesel. If you have encountered other bad please open a PR with a minimal example and some explanation what you expect to read there. This will hopefully help to improve our error messages a bit in future.
    sprhawk
    @sprhawk
    I need run conversion for one field of a Queryable Struct, datetime(mydate + strftime('%s', '2001-01-01'), 'unixepoch'), is it possible to write it in a diesel sql function style? Currently I write is in raw sql() style
    I think the problem is the nested sql function in another?
    sprhawk
    @sprhawk
    I guess I need first create a sql function inside database, then define that custom sql function in diesel with sql_function!()
    Georg Semmler
    @weiznich
    You can just define the two required sql functions via sql_function! in your rust code and then nest them as you would do the same in raw sql.
    Each function expects an Expression as argument and each functions is a Expression by itself. Just the types need to match, but that's always a requirement if you use diesel.
    sprhawk
    @sprhawk
    Thanks, I'll try it
    sansx
    @sansx
    Hi, I had copied codes from https://github.com/diesel-rs/diesel/blob/v1.3.0/examples/postgres/advanced-blog-cli/src/pagination.rs,
    I want my requests can return as paginated, seems ok, but I get some warning about adding further restricting this bound. Is there any example similar to this?
    #[derive(Deserialize, Serialize)]
    struct HttpRes<T> {
        list: Vec<T>,
        total: i64,
        page: i64,
    }
    
    fn get_pagination_res<Table, T>(
        pool: web::Data<Pool>,
        table: Table,
        page: i64,
    ) -> Result<HttpRes<T>, diesel::result::Error> 
    where
        Table: diesel::associations::HasTable,
     {
        let conn = pool.get().unwrap();
        let (list, total) = table.paginate(page).load_and_count_pages::<T>(&conn)?;
        Ok(HttpRes { list, total, page })
    }
    Georg Semmler
    @weiznich
    It's hard to tell what you are exactly looking for if you don't include any information about the actual warning and so on.
    sansx
    @sansx
    Oh, these are warnings. Thanks.
    the trait bound Table: diesel::query_builder::Query is not satisfied
    required because of the requirements on the impl of diesel::query_builder::Query for Paginated<Table>
    required because of the requirements on the impl of LoadQuery<diesel::PgConnection, (T, i64)> for Paginated<Table>rustcE0277
    handlers.rs(96, 42): consider further restricting this bound
    the trait bound Paginated<Table>: QueryId is not satisfied
    required because of the requirements on the impl of LoadQuery<diesel::PgConnection, (T, i64)> for Paginated<Table>rustcE0277
    the trait bound Table: diesel::query_builder::QueryFragment<Pg> is not satisfied
    required because of the requirements on the impl of diesel::query_builder::QueryFragment<Pg> for Paginated<Table>
    required because of the requirements on the impl of LoadQuery<diesel::PgConnection, (T, i64)> for Paginated<Table>rustcE0277
    handlers.rs(96, 42): consider further restricting this bound
    the trait bound T: diesel::Queryable<_, Pg> is not satisfied
    required because of the requirements on the impl of diesel::Queryable<(_, diesel::sql_types::BigInt), Pg> for (T, i64)
    required because of the requirements on the impl of LoadQuery<diesel::PgConnection, (T, i64)> for Paginated<Table>rustcE0277
    Georg Semmler
    @weiznich
    That are no warnings, but error messages. Basically you need to fullfil the trait bounds the compiler is mentioning them as the compiler is already telling you.
    That written: I normally advice people not to write generic code using diesel, at least not till they have fully mastered rustc generics.
    sansx
    @sansx
    Yes, It's kind of hard for me now. I should start with some easier projects.
    Georg Semmler
    @weiznich
    If you just "duplicate" these 3 lines for each call your code will just work fine + it won't be any slower.
    sansx
    @sansx
    Right, but copy them again and again is the worst choice for me, I will try to write a macro to deal with this for now, wish I can figure it out, and thanks for your patient.:D
    Georg Semmler
    @weiznich
    I'm not sure why you believe that this would be code duplication. I mean no one would consider INSERT INTO in different insert statements to be duplicated. This example is exactly the same, one with the rust representation of your SQL queries.
    That written: You could likely delegate the extraction of your connection to the webframework of your choice, which leaves exactly one line of actual code in that function. So no duplication there.
    Wagner
    @mwnDK1402
    More problems installing. This time I'm trying to use Diesel in an Amazon Linux 2 container, on AWS Lambda.
      = note: /usr/bin/ld: cannot find -lmysqlclient
              collect2: error: ld returned 1 exit status
    Here's the Dockerfile.
    FROM softprops/lambda-rust
    
    RUN yum install -y mysql-devel
    Georg Semmler
    @weiznich
    @mwnDK1402 The answer for such question does not change depending on your environment. You need to provide the necessary libraries. If the compiler emits an error stating that it cannot find the library, it is not there (at least nowhere where the compiler searches for libraries). We cannot ever cover all operating system variants out there. It's up to the user to provide a working environment. Otherwise the documentation of mysqlclient-sys list a bunch of options to modify the search path.
    Wagner
    @mwnDK1402
    Thanks, that's exactly what I was looking for.
    Wagner
    @mwnDK1402
    I haven't been able to find a pkg-config or mysql_config package for AL2.
    Georg Semmler
    @weiznich
    I'm not sure what else you expect to hear from me.
    sprhawk
    @sprhawk
    @weiznich I have a new question:
    Georg Semmler
    @weiznich
    @sprhawk There is no need to ping me for every question.
    sprhawk
    @sprhawk
    sorry
    sprhawk
    @sprhawk

    I want to write a function like composing applications with disel to wrapper following query:

    table_a::table.left_join(table_b::table)
    .select(table_a::all_columns.nullable(), table_b::all_columns.nullable())

    I can now define type using LeftJoin, but how could I define the return type for .select?

    I'm trying to use type BoxedQuery<'a> = BoxedSelectStatement<'a, (table_a::SqlType, table_b::SqlType), LeftJoin(table_a, table_b)>, but it does't work
    Georg Semmler
    @weiznich
    It's certainly possible to write such a function, but I cannot give you an example. It would certainly help if you try to elaborate exactly what you've tried so far and how this failed.
    sprhawk
    @sprhawk
    Thanks, I'll try to setup an example.
    And would you please give me a hint or doc for it? I can look at it.
    Georg Semmler
    @weiznich
    The correct way to refer to diesel types is not to use undocumented "private" symbols like BoxedSelectStatement, but the types provided by diesel::dsl.
    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