I have a pretty standard setup and want to sort by a column:
someselect.order_by(asc(table1.mycol))
However, I want rows with ''
or NULL
for mycol
to appear at the end of the results. Is there a way to do this?
SQLAlchemy has a NULLS LAST
modifier for ORDER BY
expressions:
sqlalchemy.sql.expression.nullslast(column)
See http://docs.sqlalchemy.org/en/latest/core/sqlelement.html#sqlalchemy.sql.expression.nullslast
You can use it like this:
from sqlalchemy import nullslast
# ...
someselect.order_by(nullslast(table1.mycol.asc()))
There is also nullsfirst
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With