#32584: OrderBy.as_sql() overwrites template
-------------------------------------+-------------------------------------
               Reporter:  Tim        |          Owner:  nobody
  Nyborg                             |
                   Type:             |         Status:  new
  Uncategorized                      |
              Component:  Database   |        Version:  3.1
  layer (models, ORM)                |       Keywords:  order_by,
               Severity:  Normal     |  nulls_first
           Triage Stage:             |      Has patch:  1
  Unreviewed                         |
    Needs documentation:  0          |    Needs tests:  0
Patch needs improvement:  0          |  Easy pickings:  0
                  UI/UX:  0          |
-------------------------------------+-------------------------------------
 A change in 3.1 has caused OrderBy.as_sql() in
 django.db.models.expressions to ignore sql templates provided by db
 backends when nulls_first or nulls_last is set:


 {{{
   def as_sql(self, compiler, connection, template=None, **extra_context):
         template = template or self.template
         if connection.features.supports_order_by_nulls_modifier:
             if self.nulls_last:
                 template = '%s NULLS LAST' % template
             elif self.nulls_first:
                 template = '%s NULLS FIRST' % template
         else:
             if self.nulls_last and not (
                 self.descending and
 connection.features.order_by_nulls_first
             ):
                 template = '%%(expression)s IS NULL, %s' % template
             elif self.nulls_first and not (
                 not self.descending and
 connection.features.order_by_nulls_first
             ):
                 template = '%%(expression)s IS NOT NULL, %s' % template
 }}}

 Note that template is always overwritten if nulls_first == True.  In 3.0,
 the function first tested for template == None.

 This causes trouble for the MSSQL 3rd party driver, which provides its own
 order by functionality:
 https://github.com/microsoft/mssql-django/issues/19

 The following change (simply testing for template) fixed the issue for me
 (forked off 3.1 as I'm on py 3.7)
 
https://github.com/timnyborg/django/commit/fd41b39ade8d37138951223eba7f2e3fb66d0d1c

-- 
Ticket URL: <https://code.djangoproject.com/ticket/32584>
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/052.8003a81765d6a333e11453758ac4196b%40djangoproject.com.

Reply via email to