New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Get random records from findMany() and also with pagination
#5894
Comments
This comment was marked as off-topic.
This comment was marked as off-topic.
|
Is this feature planned? |
|
I've been checking back on this issue and these docs since June 2020 or earlier, and I see that others have wanted this functionality since Feb 2017 or earlier. @schickling I'm curious whether you think anyone will work on this soon? Also, I propose an optional Sometimes it's helpful to let the client generate and store a seed (such as from a random number generator) that it can then pass to this backend function, which will then return the results sorted in a way that will always be the same (given that the full array of results hasn't changed in the meantime and the same seed was provided). See the following example of some shuffling code I've used (inspired by Mike Bostock's implementation of the Fisher–Yates algorithm): /**
*
* @param {array} array
* @param {number} seed
* @returns {array}
*/
export function shuffle(array: any, seed: number) {
// https://stackoverflow.com/a/53758827/
const result = [...array];
let a = result.length;
let b;
let elementToMove;
let currentSeed = seed;
// While there remain elements to shuffle…
while (a) {
// Pick a remaining element…
a -= 1;
b = Math.floor(random(currentSeed) * a);
// And swap it with the current element.
elementToMove = result[a];
result[a] = result[b];
result[b] = elementToMove;
currentSeed += 1;
}
return result;
} |
|
Yes, we absolutely need this feature. Please, consider adding it into the next upgrades. |
findMany and also with pagination
This comment was marked as off-topic.
This comment was marked as off-topic.
This comment was marked as off-topic.
This comment was marked as off-topic.
This comment was marked as off-topic.
This comment was marked as off-topic.
This comment was marked as off-topic.
This comment was marked as off-topic.
This comment was marked as off-topic.
This comment was marked as off-topic.
This comment was marked as off-topic.
This comment was marked as off-topic.
This comment was marked as off-topic.
This comment was marked as off-topic.
|
I will find a workaround but I am also very interested in this feature. |
|
I required this feature too, please tag me whenever there's update to this issue. Absence of advanced SQL functions is making me dismiss the usage of prisma in further projects. There must be a totally raw query option which can bypass prisma engine and send query directly to db without any syntax checking |
|
Is there any way to get only one random value after adding initial filter like where:{ }? |
findMany and also with paginationfindMany() and also with pagination
Problem
Comming from #5886.
As discussed in Prisma 1 forum, looks like there is no way to find random records with
findMany. Indeed, this is such a challenge to use withpagination.Suggested solution
Providing a
randomfunction fromprismawould be an easy solution for users therefore we can expect results withorderBy: random(). Although this results in a bad performance in puresql, I thinkPrismacan do better internally without just exposing the random inside thesql.Alternatives
If the below pagination query and raw query could aggregate, it would scope down many limitations.
The text was updated successfully, but these errors were encountered: