#27710: foreign key _id optimisation leads to incorrect descriptor value
-----------------------------------------+------------------------
               Reporter:  Will Hardy     |          Owner:  nobody
                   Type:  Uncategorized  |         Status:  new
              Component:  Uncategorized  |        Version:  1.10
               Severity:  Normal         |       Keywords:
           Triage Stage:  Unreviewed     |      Has patch:  0
    Needs documentation:  0              |    Needs tests:  0
Patch needs improvement:  0              |  Easy pickings:  0
                  UI/UX:  0              |
-----------------------------------------+------------------------
 A common optimisation (also recommended in the docs:
 https://docs.djangoproject.com/en/1.8/topics/db/optimization/#use-foreign-
 key-values-directly) is to use `MyModel.relatedfield_id = 5` for setting a
 foreign key, when you want to avoid querying the database.

 However, if the instance is then later used (accidentally or occasionally,
 that would defeat the point of the optimisation), the value of
 `MyModel.relatedfield` can be incorrect. I think inefficient would be a
 better default than incorrect, because the mistake is subtle.

 I imagine the `ForwardManyToOneDescriptor` could check the primary key of
 the cache value being returned against the primary key on the model
 instance and if there is a mismatch, then the cache value should be
 deleted and a new query should be made.

 {{{
 >>> instance = MyModel(relatedfield_id=1)
 >>> print(instance.relatedfield.pk)
 1
 >>> instance.relatedfield_id = 2
 >>> print(instance.relatedfield.pk)
 1
 >>> instance.save()
 >>> print(instance.relatedfield.pk)
 1
 }}}

--
Ticket URL: <https://code.djangoproject.com/ticket/27710>
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 django-updates+unsubscr...@googlegroups.com.
To post to this group, send email to django-updates@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-updates/052.ef661147872f5a792de44615b490b8c3%40djangoproject.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to