Skip to content Skip to sidebar Skip to footer

How To Pass A Seed Value To Random Order Function In Sequelize

In SQL dialects you can sort by random and you can pass a seed to the random function in order to get a repeatable random order of rows. In MySQL you'd do it like this: SELECT * FR

Solution 1:

If you wish your query will work only in MySQL then just use sequelize.fn:

users.findAll({
  order: [sequelize.fn('RAND', '192.168.1.1')],
});

Solution 2:

Absent any official documentation mention of the issue, the definition of the sequelize.random() method in sequelize.js (line 892) shows that it doesn't accept any parameters currently, and simply functions as a method by which to properly build a query dependent on the specific RDBMS configured for use.

As such, it is safe to say that, as of this writing, this is currently not supported in sequelize's master branch by default; @Anatoly's answer shows how you might be able to achieve this by leveraging sequelize.fn in your own project.


Post a Comment for "How To Pass A Seed Value To Random Order Function In Sequelize"