Is there a way to get a random record and/or order a query randomly with SQLmodel? #1490
Answered
by
mgurg
bolinocroustibat
asked this question in
Questions
First Check
Commit to Help
Example Coderandom_user = select(User).order_by('?').limit(1)DescriptionIs there a way to get a random record and/or order a query randomly, like an equivalent to Django Operating SystemLinux Operating System DetailsNo response SQLModel Version0.0.6 Python Version3.9 Additional ContextNo response |
Answered by
mgurg
Mar 23, 2022
Replies: 3 comments
|
Try this: from sqlalchemy import func
task = session.exec(select(Tasks).order_by(func.random())).first() |
0 replies
Answer selected by
YuriiMotov
|
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() |
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Try this: