#33531: Random ORDER BY not working with MSSQL
-------------------------------------+-------------------------------------
               Reporter:  coronoro   |          Owner:  nobody
                   Type:  Bug        |         Status:  new
              Component:  Database   |        Version:  3.0
  layer (models, ORM)                |
               Severity:  Normal     |       Keywords:
           Triage Stage:             |      Has patch:  0
  Unreviewed                         |
    Needs documentation:  0          |    Needs tests:  0
Patch needs improvement:  0          |  Easy pickings:  0
                  UI/UX:  0          |
-------------------------------------+-------------------------------------
 Given a simple Model:

 {{{
 class Person(models.Model):
     name = models.CharField(max_length=100)
     country_code = models.CharField(max_length=3, blank=True, null=True)

     class Meta:
         db_table = 'persons'
 }}}

 I want to pick an entry randomly by using

 {{{
 Person.objects.all().order_by('?')
 }}}

 the resulting query has the following order by clause

 {{{
 ... FROM [persons] ORDER BY RAND() ASC
 }}}

 This however does not give a randomized order of the table. Instead it
 always returns the same entries in the same order(ordered by id).

 The desired result can be achieved with
 {{{
 ... ORDER BY NEWID()
 }}}

 I am aware that this also isn't an exactly optimal solution as stated
 here: [https://dba.stackexchange.com/questions/955/what-is-the-best-way-
 to-get-a-random-ordering].


 For completeness: I observed this behaviour using a MSSQL14 Server

-- 
Ticket URL: <https://code.djangoproject.com/ticket/33531>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

-- 
You received this message because you are subscribed to the Google Groups 
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-updates/051.2adce5a31b42e51a031b2459a4313cdd%40djangoproject.com.

Reply via email to