#11811: No error raised on update(foreignkey=unsavedobject) on nullable fk
---------------------------------------------------+------------------------
          Reporter:  Afief                         |         Owner:  ashearer
            Status:  assigned                      |     Milestone:  1.2     
         Component:  Database layer (models, ORM)  |       Version:  1.1     
        Resolution:                                |      Keywords:          
             Stage:  Accepted                      |     Has_patch:  1       
        Needs_docs:  0                             |   Needs_tests:  0       
Needs_better_patch:  0                             |  
---------------------------------------------------+------------------------
Comment (by ashearer):

 Here are some examples to make this more concrete.

 {{{
 # Nullable foreign key field, without patch, previous surprising behavior:
 >>> s = Site(name='My new site')
 >>> f = Flatpage(id=1, text='I am being published on that site')
 >>> f.site = s              # set a site for the Flatpage
 >>> s.save()                # (site is saved now, but doesn't change
 outcome)
 >>> f.site                  # site value is still there, looks good
 <Site: My new site>
 }}}

 Old behavior from that point:
 {{{
 >>> f.save()                # save it to DB
 >>> f = Flatpage.objects.get(id=1)  # read it back
 >>> print f.site            # ...and the Flatpage's site value disappears!
 None
 }}}

 Patched behavior from that point:
 {{{
 >>> f.save()                # will now raise an exception about the
 anomaly
 Traceback (most recent call last):
 ...
 ValueError: Cannot save a foreign key referring to the instance "<Site: My
 new site>" into the field "Flatpage.site", because the key value for the
 instance was not available at the time of assignment to the field. (For
 autonumbered keys, the related instance may need to be saved prior to the
 assignment.)
 }}}


 The behavior for assignment to non-null fields with the rev-5 patch is the
 same as before:
 {{{
 # Test a non-null field. Behavior is unchanged.
 >>> d = Department(name='An Unsaved Department')
 >>> w = Worker(id=1, name='Milton Waddams', department=d)
 >>> w.save()
 Traceback (most recent call last):
 ...
 IntegrityError: model_regress_worker.department_id may not be NULL
 or
 Warning: Column 'department_id' cannot be null
 or
 other DB-specific error
 }}}

 However, the batch queryset.update() method raises a new ValueError
 exception regardless of nullability:
 {{{
 >>> d1 = Department(id=1, name='A Real Department')
 >>> d1.save()
 >>> d2 = Department(name='An Unsaved Department')
 >>> Worker.objects.create(id=2, name='Milton Waddams', department=d1)
 <Worker: Milton Waddams>
 >>> Worker.objects.filter(id=2).update(department=d2)
 Traceback (most recent call last):
 ...
 ValueError: Cannot assign the instance "<Department: An Unsaved
 Department>" to a foreign key field, because its primary key is not
 available. (For autonumbered keys, the instance would need to be saved
 first.)
 }}}

-- 
Ticket URL: <http://code.djangoproject.com/ticket/11811#comment:6>
Django <http://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