#24201: order_with_respect_to GenericForeignKey
-------------------------------------+-------------------------------------
     Reporter:  Alex Hill            |                    Owner:  nobody
         Type:  New feature          |                   Status:  closed
    Component:  Database layer       |                  Version:
  (models, ORM)                      |
     Severity:  Normal               |               Resolution:  fixed
     Keywords:                       |             Triage Stage:  Ready for
                                     |  checkin
    Has patch:  1                    |      Needs documentation:  0
  Needs tests:  0                    |  Patch needs improvement:  0
Easy pickings:  0                    |                    UI/UX:  0
-------------------------------------+-------------------------------------

Comment (by Ashley Waite):

 Replying to [comment:16 Simon Charette <charette.s@…>]:
 > In [changeset:"c0118ff80bd14fe0f65843e5df266ec5d922304d" c0118ff8]:
 > {{{
 > #!CommitTicketReference repository=""
 revision="c0118ff80bd14fe0f65843e5df266ec5d922304d"
 > Refs #24201 -- Ignored order_with_respect_to private fields in
 migrations.
 >
 > Thanks Tim for the review.
 > }}}

 This commit removes the {{{order_with_respect_to}}} option during
 migration state if it's a generic foreign key.
 I can see why this was done, as private fields are not present on the
 model state, so it cannot be resolved.

 However, as the option is removed, no {{{_order}}} field is created in the
 database and no {{{AlterOrderWithRespectTo}}} operations can be generated
 for the migration either.
 An {{{AlterOrderWithRespectTo}}} operation cannot be manually added either
 because the migrations {{{ModelState}}} does not copy private fields
 either, so attempting to add it results in referring to a field it cannot
 find.
 Manually adding an {{{AlterOrderWithRespectTo}}} operation referring to
 another field will result in it being removed in any subsequent migration.

 When you attempt to use the model it hard breaks because there's no
 {{{_order}}} field in the database.

 {{{
 psycopg2.ProgrammingError: column test_model._order does not exist
 django.db.utils.ProgrammingError: column test_model._order does not exist
 }}}

 This requires several changes in the migration module to resolve
 _correctly_, but can be hackishly bypassed by replacing
 
[[https://github.com/django/django/blob/c0118ff80bd14fe0f65843e5df266ec5d922304d/django/db/migrations/state.py#L446-L448|state.py#446-448]]
 with:
 {{{
         # Private fields are ignored, so remove options that refer to
 them.
         elif options.get('order_with_respect_to') in (field.name for field
 in model._meta.private_fields):
             options['order_with_respect_to'] = model._meta.pk.attname
 }}}

 Which leads to the creation of {{{_order}}} and
 {{{AlterOrderWithRespectTo}}} operations, thinking that it's ordering
 according to the {{{id}}} field, but as it migrates and functions
 successfully, this is the clear source of why it currently breaks.

-- 
Ticket URL: <https://code.djangoproject.com/ticket/24201#comment:17>
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 post to this group, send email to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-updates/066.191ac103bb70847a0c370861fa3113cb%40djangoproject.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to