-
Notifications
You must be signed in to change notification settings - Fork 1.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Allow sorting by NULLS FIRST / NULLS LAST #4368
Comments
Need this. Any updates on this functionality? |
I would like this feature too. |
Need this, would prevent having to convert a large prisma query to a raw query every time I want to add a sort with nulls last/first |
Need this... |
Need this aswell... :) |
Is this a "good first issue"? |
We had to eject out and use raw sql for this 😿 |
+1 also looking for this |
I think it would be best to look toward Hasura for the solution to this, add a few more values to the SortOrder enum: |
Any idea of when this will be possible? |
I agree with @dariusj18 – this would then also automatically propagate to the Go client. |
why did this problem take 2 years already? I think it is like hardcoded or something dariusj18 said O_O. |
To not break existing, we could just add the 2 missing value in the sorting enum? |
My guess is that the only difficulty would lie in the database support, so postgresql is the only one that would be simple out of the box. Most other databases require adding an additional CASE statement to the ORDER BY to force null sort weight, and mongodb requires other hacks. Which is why it would be nice to have that abstracted away in this library. |
You are right. ✅ But, maybe it could be implemented in postgresql fast and leave complicated stuff for later? :) |
Your analysis is fully on point. As Prisma supports multiple databases, we can not easily just add the "simple" one and ignore the other ones. When a feature exists, it is hard to take it back - so we need to understand what it means (e.g. "not possible" or "super hard to build and maintain") for all the other databases. You can help us by understanding the complexities this would create for all our other databases, and leave example and thoughts here as a comment. If we have most of the "logic" already figured out for a feature, it is of course much easier to tackle it. |
All right, let's try 💪 Let's consider
PostgreSQLASCSELECT * FROM tabletosort order by name -- default to NULLS LAST
-- How to put null first?
-- Option Native NULLS FIRST
SELECT * FROM tabletosort order by name NULLS FIRST
-- Option col IS NULL then
SELECT * FROM tabletosort order by name IS NULL desc, name DESCSELECT * FROM tabletosort order by name desc -- default to NULLS FIRST
-- How to put null last?
-- Option Native NULLS LAST
SELECT * FROM tabletosort order by name desc NULLS LAST
-- Option col IS NULL then
SELECT * FROM tabletosort order by name IS NULL, name desc MySQLASCSELECT * FROM tabletosort order by name -- default to NULLS FIRST
-- Option col IS NULL then
-- it's supported DESCSELECT * FROM tabletosort order by name desc -- default to NULLS LAST
-- Option col IS NULL then
-- it's supported SQLiteASCSELECT * FROM tabletosort order by name -- default to NULLS FIRST
-- Option col IS NULL then
-- it's supported DESCSELECT * FROM tabletosort order by name desc -- default to NULLS LAST
-- Option col IS NULL then
-- it's supported Microsoft SQL ServerASCSELECT * FROM tabletosort order by name -- default to NULLS FIRST
-- Option col IS NULL then
-- it's supported DESCSELECT * FROM tabletosort order by name desc -- default to NULLS LAST
-- Option col IS NULL then
-- it's supported Summary
Proposaltype orderBy = {
direction:
| 'asc' // platform asc
| 'desc' // platform desc
| 'asc_nulls_first' // force null first then asc
| 'asc_nulls_last' // force null last then asc
| 'desc_nulls_first' // force null first then desc
| 'desc_nulls_last'; // force null last then desc
};
...
await this.prisma.tabletosort.findMany({
orderBy: { nom: 'asc' }
}); Like this, there is NO change of the current behavior and you can opt-in a specific sorting. 🤞 that the topic can move forward 🥳 |
@jycouet's proposal looks amazing, please look into this, this is a very common case! |
For MS SQL Server you can use |
To bad that In the meantime, I have a workaround to trick the system that I want to share.
This trick is a also a nice candidate for |
This makes a lot of sense as a workaround! Just wanted to drop a note that may be useful:
Postgres does have support for infinite timestamps, so it may be a little more strictly correct to use them if there's worries about representing really old dates correctly. https://www.postgresql.org/docs/current/datatype-datetime.html#DATATYPE-DATETIME-SPECIAL-VALUES |
Great news!!! |
Problem
Currently can't seem to specify how to sort nulls in a findMany
Suggested solution
To be able to specify NULLS FIRST or NULLS LAST
Alternatives
Currently making separate queries and concat-ing them in JS
Additional context
On a table with rows that contain data synced from elsewhere, there's a date field for
last_synced_details_at
;The column's default value is
null
(never been synced)When attempting to get the "most stale rows" would be
orderBy: { last_synced_details_at: "asc" }
, I'd get the ones that have been synced first, and rows that have never been synced afterThe text was updated successfully, but these errors were encountered: