#14615: Related objects manager returns related objects with null FKs for 
unsaved
instances
-------------------------------------+-------------------------------------
     Reporter:  tonnzor              |                    Owner:
         Type:  Bug                  |                   Status:  reopened
    Component:  Database layer       |                  Version:  1.2
  (models, ORM)                      |               Resolution:
     Severity:  Normal               |             Triage Stage:  Accepted
     Keywords:                       |      Needs documentation:  0
    Has patch:  1                    |  Patch needs improvement:  1
  Needs tests:  0                    |                    UI/UX:  0
Easy pickings:  0                    |
-------------------------------------+-------------------------------------
Changes (by lukeplant):

 * needs_better_patch:  0 => 1
 * stage:  Design decision needed => Accepted


Comment:

 According to wikipedia and this discussion:
 https://groups.google.com/forum/#!msg/django-
 developers/sCjjx6NmMw4/4VJ4D0vsA_kJ

 ...having a NULL primary key is not allowed. SQLite allows it, but doesn't
 guarantee it will moving forward. Therefore, we should not program for
 that edge case. Instead, we can detect unsaved instances using the
 existing 'pk is None' idiom.

 (If we make a decision that NULL PKs *are* allowed, there are lots of bit
 of code that probably need updating, and that should be done separately -
 although that seems unlikely).

 The `instance._state.adding` flag was needed for the edge case of doing
 uniqueness validation for explicitly set `CharField` PKs for new objects.
 That is not needed here, or wanted - if the user creates a Model instance
 with an explicitly set PK which corresponds to data in the database, we
 expect that doing the related lookups should return the records related to
 that PK e.g. we expect `User(id=1).myobject_set.all()` to return
 `MyObject.objects.filter(user_id=1)`, even though the User instance was
 not saved.

 Apologies for the confusion my patch added.

-- 
Ticket URL: <https://code.djangoproject.com/ticket/14615#comment:14>
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 post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/django-updates?hl=en.

Reply via email to