[BangPypers] Getting a random record from Database . The best strategy.
dhruvbaldawa at gmail.com
Fri Oct 4 09:24:09 CEST 2013
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;
I have not tested the code myself though.
On Thu, Oct 3, 2013 at 10:11 PM, Amit Sethi <amit.pureenergy at 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
> 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
> 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.
> A-M-I-T S|S
> BangPypers mailing list
> BangPypers at python.org
More information about the BangPypers