Skip to content

Conversation

RomainLanz
Copy link
Member

Hey there! 👋🏻

This PR adds a orderByRandom method to retrieve records in a random order.
It defines the right keyword for each dialect.

The test may be flaky, I have set it up to run 3 times in case it fails.

A seed can be pass when using MySQL (same as Laravel implementation).

@RomainLanz
Copy link
Member Author

I am not sure how to deal with the mssql error. We are not passing multiple NEWID() to the order by clause. 🤔

Comment on lines +5459 to +5483
const db = getQueryBuilder(getQueryClient(connection))
await getInsertBuilder(getQueryClient(connection))
.table('users')
.multiInsert([
{
username: 'virk',
email: '[email protected]',
},
{
username: 'romain',
email: '[email protected]',
},
{
username: 'nikk',
email: '[email protected]',
},
])

const userResults: number[][] = []

for (let i = 0; i < 10; i++) {
const result = await db.from('users').orderByRandom()

userResults.push(result.map((user) => user.id))
}

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It looks like it was reusing the same query builder, so it would carry over the previous order by.

I found that if you use a new query builder within the for loop, it works as expected:

Suggested change
const db = getQueryBuilder(getQueryClient(connection))
await getInsertBuilder(getQueryClient(connection))
.table('users')
.multiInsert([
{
username: 'virk',
email: '[email protected]',
},
{
username: 'romain',
email: '[email protected]',
},
{
username: 'nikk',
email: '[email protected]',
},
])
const userResults: number[][] = []
for (let i = 0; i < 10; i++) {
const result = await db.from('users').orderByRandom()
userResults.push(result.map((user) => user.id))
}
const client = getQueryClient(connection)
await getInsertBuilder(getQueryClient(connection))
.table('users')
.multiInsert([
{
username: 'virk',
email: '[email protected]',
},
{
username: 'romain',
email: '[email protected]',
},
{
username: 'nikk',
email: '[email protected]',
},
])
const userResults: number[][] = []
for (let i = 0; i < 10; i++) {
const result = await getQueryBuilder(client).from('users').orderByRandom()
userResults.push(result.map((user) => user.id))
}

Or clear the order by clauses like so:

const result = await db.from('users').clearOrder().orderByRandom()

Copy link

stale bot commented Apr 26, 2025

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

@stale stale bot added the Status: Abandoned Dropped and not into consideration label Apr 26, 2025
@stale stale bot closed this May 6, 2025
@RomainLanz RomainLanz reopened this May 6, 2025
@stale stale bot removed the Status: Abandoned Dropped and not into consideration label May 6, 2025
Copy link

stale bot commented Jul 18, 2025

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

@stale stale bot added the Status: Abandoned Dropped and not into consideration label Jul 18, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Status: Abandoned Dropped and not into consideration
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants