Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQLAlchemy Core: order by desc

Tags:

What is the proper way to execute a select statement with an "ORDER BY foo DESC" in SQLAlchemy core? (core, not ORM!)

I'm currently including the direction in the order_by directly:

mytable.select(order_by='name DESC')

...it works (although I don't like it since it's a little bit "hackish"), but SQLAlchemy gives me the following warning:

SAWarning: Can't resolve label reference 'name DESC'; converting to text() (this warning may be suppressed after 10 occurrences)
util.ellipses_string(element.element))

(I didn't find anything about in the doc)

like image 455
daveoncode Avatar asked Feb 01 '16 14:02

daveoncode


People also ask

What does all () do in SQLAlchemy?

all() method. The Query object, when asked to return full entities, will deduplicate entries based on primary key, meaning if the same primary key value would appear in the results more than once, only one object of that primary key would be present.

What does query all () return?

all() will return all records which match our query as a list of objects.

What is subquery in SQLAlchemy?

The statement ends by calling subquery() , which tells SQLAlchemy that our intention for this query is to use it inside a bigger query instead of on its own.

How do I SELECT a column in SQLAlchemy?

SQLAlchemy Core The already created students table is referred which contains 4 columns, namely, first_name, last_name, course, score. But we will be only selecting a specific column. In the example, we have referred to the first_name and last_name columns. Other columns can also be provided in the entities list.


1 Answers

From the SQLAlchemy docs:

from sqlalchemy import desc

stmt = select([users_table]).order_by(desc(users_table.c.name))
like image 109
Wayne Werner Avatar answered Sep 20 '22 03:09

Wayne Werner