question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

ORDER BY a json field fails in CTE select_from

See original GitHub issue
class Foo(Model):
    data = BinaryJSONField()

# OK: Produces SELECT ... ORDER BY ("foo"."data"->>'x');
Foo.select().order_by(Foo.data['x'])

# NOK: Produces WITH foos AS (...) SELECT ... ORDER BY ("foos"."data" = 'x');
cte = Foo.select().cte('foos')
query = cte.select_from().order_by(cte.c.data['x'])

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:8 (5 by maintainers)

github_iconTop GitHub Comments

2reactions
coleifercommented, Aug 2, 2018

Ouch…that’s going to be rather difficult to fix, unfortunately, perhaps not possible. I’d suggest in the meantime trying something like:

.order_by(Expression(cte.c.data, '->>', SQL("'x'")))
0reactions
coleifercommented, Aug 13, 2018

Good suggestions, thank you.

Read more comments on GitHub >

github_iconTop Results From Across the Web

sql - How to get the second highest to lowest value record on ...
Here I have a JSON field I want to ...
Read more >
Working with CTEs (Common Table Expressions)
A CTE (common table expression) is a named subquery defined in a WITH clause. ... The CTE defines the temporary view's name, an...
Read more >
Solve common issues with JSON in SQL Server
I want to create a JSON text result from a simple SQL query on a single ... FOR JSON queries as column expressions...
Read more >
Common Table Expressions (WITH Queries) - Cockroach Labs
A common table expression (CTE), also called a WITH query, ... In this example, the inner subquery SELECT * FROM v will select...
Read more >
16.20 - Examples: SELECT AS JSON - Teradata Database
Following is the table definition for these examples. CREATE TABLE MyTable ( a INTEGER, b INTEGER, j JSON AUTO COLUMN); These statements ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found