You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Reacted by Leandro Hoffmann, Jonathan Uhls, Tom Bailey, Mostafa Farghaly, Renato Gama, omkarpg0931, gerenrot, Paul-DS, Manny 5th, Francis Marineau and 37 more
It's not supported and is not likely to ever be.
How would a limit like that even work honestly? We already do a subquery to limit the inner row, we would have to do a id fetch subquery for the join row to be able to limit it.
Can you suggest how a SQL query that achieves what you want might look?
Maybe it could be achieved by joining through SELECT subquery. So instead of:
SELECT "Table1"."id" AS "Table1.id", "Table2"."id" AS "Table2.id"
FROM "Table1"
LEFT JOIN "Table2"
ON "Table1"."id" = "Table2"."Table1Id"
LIMIT 50
This query which generates join table base on join conditions
SELECT "Table1"."id" AS "Table1.id", "Table2"."id" AS "Table2.id"
FROM "Table1"
LEFT JOIN (
SELECT * FROM "Table2" WHERE "Table2"."Table1Id" = "Table1"."id" LIMIT 5
) AS "Table2"
ON "Table1"."id" = "Table2"."Table1Id"
LIMIT 50
I am not sure it thats is going to work, but if yes then it would be very handy feature :)
I know I have been neglecting the other issues that I have open, work has picked up and made me rather busy. But I am very interested in being able to limit an include as well. :)
In fact, limiting an include is integral to our app's functionality, otherwise I will have to do it by looping through and doing another query for each result of the first query.
The action I have been attempting is that I want to findAll of a user object, but include the most recent of a user:many association. Previously I have ordered by createdAt and used limit 1, but I cannot do this with an include.
@Roam-Cooper yeah that's likely the way we go, but it has to support where clauses in there aswell so it's a completely new set of logic in the join statements - So not entirely trivial, but doable.
Same here, I have a belongsToMany relation of orders and order changes, where I keep in orders its id, and order changes, anything that changes for that order, keeping a history of changes.
But, in my view I just need the latest change, so, I can limit my order history include through an order clause on created_at, desc then limit it to 1.
Is that possible today? Could not find it in the docs.
As of now I order by desc, iterate through its elements , get the last one by index, and ignore previous ones.
I wouldn't want to do that as the more histories I get, more ram I will need to load the info, having it limited to 1 makes more sense.
Reacted by Hu Zheng, Ali Alkhatib, Ivan Tershukov, Julien Ambrosio, JasonChou, Tatiana C. Molina Higgins, Karan Kotwal, Anton, Behcet, Katie Lane and 1 more
I also need to limit results, retrieved by findAll with include statement.
I have thousands of companies, where each company belongs to many City.
City belongs to Region, Region belongs to Country.
I need to get the last 10 companies for specific Country.
When a company is associated with 3 cities, I will get 7 companies in query result, instead of 10, because company in question will be duplicated 3 times.
The problem is that I cannot just take a little greater LIMIT value "in advance", because company can be associated with 100+ cities (getting 100 duplicates in result).
LIMIT will work, when in the SQL-query above I will rewrite SELECT statement to output only columns of company table and use DISTINCT keyword:
Not the best solution, but following @AntonAL 's logic, you can add:
if (options.distinct) { query = query.replace('SELECT', 'SELECT distinct') }
to the node_module at sequelize/lib/dialects/abstract/query-generator.js line 1366.
This allows you to add distinct: true to your findAll.
I have tested this with Postgres and it seems to be working great. Thank you @AntonAL for finding that distinct works. This would be a great PR if anyone has the time to test it out.
Activity
[-]how to limit findall include limit[/-][+]how to set findall include model limit?[/+]mickhansen commentedon Jun 13, 2014
It's not supported and is not likely to ever be.
How would a limit like that even work honestly? We already do a subquery to limit the inner row, we would have to do a id fetch subquery for the join row to be able to limit it.
Can you suggest how a SQL query that achieves what you want might look?
alekbarszczewski commentedon Jun 14, 2014
Maybe it could be achieved by joining through SELECT subquery. So instead of:
This query which generates join table base on join conditions
I am not sure it thats is going to work, but if yes then it would be very handy feature :)
mickhansen commentedon Jun 16, 2014
@alekbarszczewski hmm, perhaps that could indeed work.
Roam-Cooper commentedon Jul 2, 2014
I know I have been neglecting the other issues that I have open, work has picked up and made me rather busy. But I am very interested in being able to limit an include as well. :)
In fact, limiting an include is integral to our app's functionality, otherwise I will have to do it by looping through and doing another query for each result of the first query.
The action I have been attempting is that I want to findAll of a user object, but include the most recent of a user:many association. Previously I have ordered by createdAt and used limit 1, but I cannot do this with an include.
The way that Alek said to do it is the way that most people seem to be doing it as in http://stackoverflow.com/questions/13525656/limit-a-left-join-on-the-first-table and many other stackoverflow threads.
mickhansen commentedon Jul 3, 2014
@Roam-Cooper yeah that's likely the way we go, but it has to support where clauses in there aswell so it's a completely new set of logic in the join statements - So not entirely trivial, but doable.
Roam-Cooper commentedon Jul 6, 2014
@mickhansen I'd make an attempt at doing it myself, but I've never had a look at the internals of Seq before. :)
seth-admittedly commentedon Jul 7, 2014
This would hopefully also resolve #1756
mickhansen commentedon Jul 7, 2014
@seth-admittedly Hmm, unfortuneatly i don't think so.
kmsheng commentedon Oct 8, 2014
I need this, too.
luccasmaso commentedon Nov 14, 2014
+1
daimajia commentedon Nov 20, 2014
need += 1
vilmosioo commentedon Nov 28, 2014
+1
kdelemme commentedon Nov 28, 2014
This would be an awesome feature!
40 remaining items
ernani commentedon Aug 19, 2017
Same here, I have a belongsToMany relation of orders and order changes, where I keep in orders its id, and order changes, anything that changes for that order, keeping a history of changes.
But, in my view I just need the latest change, so, I can limit my order history include through an order clause on created_at, desc then limit it to 1.
Is that possible today? Could not find it in the docs.
As of now I order by desc,
iterate through its elements, get the last one by index, and ignore previous ones.I wouldn't want to do that as the more histories I get, more ram I will need to load the info, having it limited to 1 makes more sense.
AntonAL commentedon Apr 10, 2018
I also need to limit results, retrieved by
findAllwithincludestatement.I have thousands of companies, where each company belongs to many City.
City belongs to Region, Region belongs to Country.
I need to get the last 10 companies for specific Country.
The code is following:
It generates following SQL
When a company is associated with 3 cities, I will get 7 companies in query result, instead of 10, because company in question will be duplicated 3 times.
The problem is that I cannot just take a little greater
LIMITvalue "in advance", because company can be associated with 100+ cities (getting 100 duplicates in result).LIMIT will work, when in the SQL-query above I will rewrite
SELECTstatement to output only columns ofcompanytable and useDISTINCTkeyword:How can I omit the output of associated tables colums in top-level SELECT-query?
AntonAL commentedon Apr 10, 2018
Figured out, how to get rid of
"cities","cities->region","cities->region->country"columns.Resulting SQL is now following:
Now looking, how to:
"cities->companyCity"columns…;DISTINCTstatement.Any ideas?
AntonAL commentedon Apr 10, 2018
It's strange, but If I rewrite Sequelize query, including intermediate join table, like this:
The part is still present in
SELECTstatement of SQL-query:AntonAL commentedon Apr 10, 2018
Also, an advice, described here #3664 (comment), does't work:
rivers-dash commentedon Nov 7, 2018
Hello, I think it's working if u put the order properties in an array
order : [ ['article.created_at', 'DESC'], ],mi-mazouz commentedon Nov 8, 2018
any updates?
jacobbodkin commentedon Jun 20, 2019
Not the best solution, but following @AntonAL 's logic, you can add:
if (options.distinct) { query = query.replace('SELECT', 'SELECT distinct') }to the node_module at sequelize/lib/dialects/abstract/query-generator.js line 1366.
This allows you to add
distinct: trueto your findAll.I have tested this with Postgres and it seems to be working great. Thank you @AntonAL for finding that distinct works. This would be a great PR if anyone has the time to test it out.