query_builder icon indicating copy to clipboard operation
query_builder copied to clipboard

Querybuilder is modifying original query.

Open collegeimprovements opened this issue 4 years ago • 2 comments

Thanks a lot for this awesome package. It has simplified searching for us.

iex(9)> query = from c in Country, limit: 2
#Ecto.Query<from c0 in ClocheApi.Locations.Country, limit: ^2>

iex(10)> QueryBuilder.order_by(query, desc: :inserted_at)              
#Ecto.Query<from c0 in ClocheApi.Locations.Country,
 as: ClocheApi.Locations.Country, order_by: [desc: c0.inserted_at]>

☝🏼 As shown above, the original query has limit in it. However when we use order_by it removes the limit and modifies the original query instead of enhancing it.

collegeimprovements avatar Aug 25 '21 13:08 collegeimprovements

Note that limit/offset have been added: https://github.com/mathieuprog/query_builder/issues/7

The problem with supporting an existing query like above, is that there might be joins/bindings that QueryBuilder then won't be able to use.

To address this issue, I see the following solutions:

  • specify in the documentation that this is not supported
  • raise an error if we see that a query is passed like above (as opposed to a schema name as shown in the docs)
  • find a solution for the binding names
  • accept a query as shown above but only if it doesn't come with joins/bindings (i.e. accept query but limited support)

mathieuprog avatar Sep 22 '21 12:09 mathieuprog

Thanks for the reply. Maybe we can document that it's always best to use limit and order_by at the end of the query. I ended up making a custome order_by for our app which just considers top-level order_bys.

collegeimprovements avatar Oct 01 '21 04:10 collegeimprovements