Open bolinocroustibat opened 2 years ago
Try this:
from sqlalchemy import func
task = session.exec(select(Tasks).order_by(func.random())).first()
I ended up here looking at how to find the last record. Docs and issues don't have info on this. Thanks for the direction.
Using the code from this issue finding the last record (based on the primary key), you just have to sort the results in descending order with sqlalchemy imports and return first as well:
from sqlalchemy import asc, desc
task = session.exec(select(Tasks).order_by(desc(Tasks.id)).first()
First Check
Commit to Help
Example Code
Description
Is there a way to get a random record and/or order a query randomly, like an equivalent to Django
.order_by('?')
or to SQLalchemy.order_by(func.rand())
for MySQL?Operating System
Linux
Operating System Details
No response
SQLModel Version
0.0.6
Python Version
3.9
Additional Context
No response