Skip to content

Order by random #605

Open
Open

Description

@bolinocroustibat

Is there a way to get a random record or order randomly with Ormar ORM?
I couldn't find anything about that in the documentation, so I tried SQLalchemy's .order_by(func.rand()) and .order_by(func.random()). I also tried the Django ORM's way .order_by('?') in case, with no success.

So, for example the following raises an error:

from sqlalchemy.sql.expression import func

random_user = await Users.objects.order_by(func.rand()).limit(1).all()

Database is MySQL 8 - but question applies also for PostgreSQL 14
Python version is 3.9
Ormar version is 0.10.25
FastAPI version is 0.74.1
Pydantic version is 1.9.0

(Sorry I'm not sure if this is a documentation issue, a tech issue, a question, or a potential enhancement. Don't hesitate to change the label if needed.)

Activity

  1. changed the title [-]Order by random[/-] [+]Order by random raises error[/+] on Mar 29, 2022
  2. changed the title [-]Order by random raises error[/-] [+]Order by random[/+] on Mar 29, 2022
  3. added
    enhancementNew feature or request
    and removed
    bugSomething isn't working
    on Apr 12, 2022
  4. PackingCenter commented on Oct 4, 2023

    @PackingCenter

    so, is it possible to get N random rows with ormar in query?
    (without getting all records and selecting N random from them)?

  5. Phizilion commented on Feb 22, 2024

    @Phizilion

    Still need it

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions