0

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

Rangel
  • 11
  • 2

2 Answers2

0

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

coleifer
  • 24,887
  • 6
  • 60
  • 75
0

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....

ron_g
  • 1,474
  • 2
  • 21
  • 39