jeudi 28 novembre 2019

How to reproducibly query random rows with SQLAlchemy from PostgreSQL?

I am trying to pseudo-randomly select rows from a PostgreSQL table using SQLAlchemy, but I need to use a seed to guarantee reproducibility of the query. The specific case is concerning a publication being submitted, tied to a codebase.

This answer does a great job introducing how one can leverage the following one-liner to select a single random row:

select.order_by(func.random()) # for PostgreSQL, SQLite

Further, one can select many of psuedo-random rows via:

select.order_by(func.random()).limit(n)

But how do I ensure that I select the same psuedo-random rows every time I run the above query?




Aucun commentaire:

Enregistrer un commentaire