#32775: LiveServerTestCase fails when query expressions are used for model 
ordering
-----------------------------------+------------------------------------
     Reporter:  Gergely Kalmár     |                    Owner:  nobody
         Type:  Bug                |                   Status:  new
    Component:  Testing framework  |                  Version:  3.2
     Severity:  Normal             |               Resolution:
     Keywords:                     |             Triage Stage:  Accepted
    Has patch:  0                  |      Needs documentation:  0
  Needs tests:  0                  |  Patch needs improvement:  0
Easy pickings:  0                  |                    UI/UX:  0
-----------------------------------+------------------------------------
Changes (by Simon Charette):

 * stage:  Unreviewed => Accepted


Comment:

 You'll want to make sure to generate migrations for the app containing
 these models otherwise you won't be able to reproduce.

 This bug is two fold. First we likely just want to have the querysets
 generated during serialization order by the primary key without involving
 possible related object semantic

 {{{#!python
 diff --git a/django/db/backends/base/creation.py
 b/django/db/backends/base/creation.py
 index 81cb34bd9f..f49c31edaf 100644
 --- a/django/db/backends/base/creation.py
 +++ b/django/db/backends/base/creation.py
 @@ -129,7 +129,7 @@ def get_objects():
                          ):
                              queryset = model._base_manager.using(
                                  self.connection.alias,
 -                            ).order_by(model._meta.pk.name)
 +                            ).order_by('pk')
                              yield from queryset.iterator()
          # Serialize to a string
          out = StringIO()
 }}}

 The above happens to address the reported crash but it doesn't deal with
 the fundamental issue which is that
 `Referent.objects.order_by('reference')` crashes if
 `Referred.Meta.ordering` contains expressions because they won't be
 transposed to the origin of the queryset (e.g. `Lower('id') ->
 Lower('order__id')` in the reported case). It can be reproduced with the
 same set of models and the following test


 {{{#!python
 from django.test import TestCase

 from .models import Offer

 class Test(TestCase):
     def test_case(self):
         list(Offer.objects.order_by('order'))
 }}}

 The issue lies in `SQLCompiler.find_ordering_name` but
 
[https://github.com/django/django/blob/5e04e84d67da8163f365e9f5fcd169e2630e2873/django/db/models/sql/compiler.py#L773-L774
 is not trivial to address] as we'd need a way to prefix all field
 references in an `Expression` so I'd suggest we raise a
 `NotImplementedError` instead for now. [https://djangoci.com/job/django-
 coverage/HTML_20Coverage_20Report/_home_jenkins_workspace_django-
 coverage_django_db_models_sql_compiler_py.html#t774 Not sure when this
 path is covered by the suite] but I would assume it would only work when
 MTI inheritance is involved and referenced field references don't need to
 be prefixed as they are inherited?

-- 
Ticket URL: <https://code.djangoproject.com/ticket/32775#comment:2>
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/071.7561efcbdaf80affbb6f4e2d7c5a2b66%40djangoproject.com.

Reply via email to