Rangel
Rangel

Reputation: 11

Test peewee fn.rand error

I'm writing the API tests and when I use peewee function fn.Rand() I get this error:

cursor.execute(sql, params or ())
OperationalError: no such function: Rand

My code is:

query = Questions.select().order_by(fn.Rand()).limit(limit)
list = [ob.as_json() for ob in query]

Any ideia how to resolve?

Tks

Upvotes: 0

Views: 496

Answers (2)

ron_g
ron_g

Reputation: 1663

Postgresql and Sqlite use the Random function:

# Pick 5 lucky winners:
LotteryNumber.select().order_by(fn.Random()).limit(5)

MySQL uses Rand:

# Pick 5 lucky winners:
LotterNumber.select().order_by(fn.Rand()).limit(5)

According to the docs anyway....

Upvotes: 0

coleifer
coleifer

Reputation: 26245

Depending on the database, the function may be called "Random". Try changing your code to fn.Random().

Upvotes: 0

Related Questions