Skip to content
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

NULLS LAST added to query in django 3.1 #19

Closed
timnyborg opened this issue Mar 22, 2021 · 4 comments
Closed

NULLS LAST added to query in django 3.1 #19

timnyborg opened this issue Mar 22, 2021 · 4 comments

Comments

@timnyborg
Copy link
Contributor

timnyborg commented Mar 22, 2021

I'm testing upgrading from django 3.0 to 3.1, using the latest backend, version 1.0b1. When using the nulls_last flag in an order_by, it appends a bit of SQL invalid in MSSQL (NULLS LAST)

The following use of nulls_last:

def other_runs(self):
    return (
        Module.objects
        .filter(url=self.url, division=self.division)
        .exclude(id=self.id)
        .order_by(F('start_date').desc(nulls_last=True))
    )

Produces the query:

SELECT TOP 21 [module].[id], ... FROM [module] WHERE ([module].[division] = %s AND [module].[url] = %s 
AND NOT ([module].[id] = %s)) ORDER BY CASE WHEN [module].[start_date] IS NULL THEN 1 ELSE 0 END, 
[module].[start_date] DESC NULLS LAST

So both the MSSQL workaround and the ANSI SQL standard are there.

Naturally, this throws the error [42000] [Microsoft][ODBC Driver 17 for SQL Server][SQL Server]Incorrect syntax near 'NULLS'. (102) (SQLExecDirectW)

@timnyborg
Copy link
Contributor Author

After digging into the issue, I found the backend was just missing a couple more feature flags, so I've opened pull request #20

@absci
Copy link
Contributor

absci commented Mar 24, 2021

Thanks for the pull request, we'll review it as soon as we can.

@absci
Copy link
Contributor

absci commented Mar 26, 2021

Thank you for your contribution, this was merged.

@davidjb
Copy link
Contributor

davidjb commented Apr 19, 2021

This is still an issue, but in a different way, see #31 here and I've reopened https://code.djangoproject.com/ticket/32584 and expanded the description and context.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants