from sqlalchemy.sql.expression import func
random_row = session.query(YourModel).order_by(func.random()).first()

this is equivalent to:
SELECT * FROM my_table ORDER BY RAND() LIMIT 1;
http://www.kavoir.com/2009/03/sql-randomly-shuffle-rows-or-records-reorder-them-in-a-random-order.html

I have not tested the code myself though.


--
Dhruv Baldawa
(http://www.dhruvb.com)


On Thu, Oct 3, 2013 at 10:11 PM, Amit Sethi <amit.pureene...@gmail.com>wrote:

> Hi ,  I am not sure if this question will be considered sufficiently
> python related. If its not than I am sorry I will take this somewhere
> else
>
> I am setting up some smoke tests for which I am fetching some records
> from the db.
>
> I am using sqlalchemy for this right now.
> My real question when fetching such a random record from a db for some
> reason.
> What is the best strategy.
>
> Should I use some  kind of randomizer in the database tool.
> Or should I use something in ORM
> Or should I make the decision for finding the random value in the
> python or the language I am working with and then fetch the record
> using the tools I am using.
>
>
> Thanks
> Amit
>
> --
> A-M-I-T S|S
> _______________________________________________
> BangPypers mailing list
> BangPypers@python.org
> https://mail.python.org/mailman/listinfo/bangpypers
>
_______________________________________________
BangPypers mailing list
BangPypers@python.org
https://mail.python.org/mailman/listinfo/bangpypers

Reply via email to