0 votes
1 view
in Python by (47.8k points)

How can I use ORDER BY descending in an SQLAlchemy query like the following?

This query works, but returns them in ascending order:

query = (model.Session.query(model.Entry) .join(model.ClassificationItem)

.join(model.EnumerationValue)

.filter_by(id=c.row.id)
.order_by(model.Entry.amount) # This row :)

)

If I try:

.order_by(desc(model.Entry.amount))

then I get:

NameError: global name 'desc' is not defined.

1 Answer

0 votes
by (107k points)

There are many ways of using ORDER BY descending in an SQLAlchemy but some of the important ways I am discussing here:-

You can import desc from the sqlalchemy package and use the function someselect.order_by() function it will do the task.

from sqlalchemy import desc someselect.order_by(desc(table1.mycol))

Another way you can do is to specify those things as column attributes. Like I am mentioning below. This way becomes handy when you see the relation definition type of things:-

.order_by(model.Entry.amount.desc())

Related questions

0 votes
1 answer
0 votes
1 answer
0 votes
2 answers
asked Jul 22, 2019 in Python by Sammy (47.8k points)
0 votes
1 answer
Welcome to Intellipaat Community. Get your technical queries answered by top developers !


Categories

...