Django

Code

Changeset 8100

Show
Ignore:
Timestamp:
07/26/08 23:18:52 (4 months ago)
Author:
mtredinnick
Message:

Fixed #7778 -- Fixed a tricky case of foreign key clearing with inherited
models. Patch from James Murty.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • django/trunk/AUTHORS

    r8088 r8100  
    281281    mrmachine <real.human@mrmachine.net> 
    282282    Robin Munn <http://www.geekforgod.com/> 
     283    James Murty 
    283284    msundstr 
    284285    Robert Myers <myer0052@gmail.com> 
  • django/trunk/django/db/models/query.py

    r8098 r8100  
    837837        update_query = sql.UpdateQuery(cls, connection) 
    838838        for field in cls._meta.fields: 
    839             if field.rel and field.null and field.rel.to in seen_objs: 
     839            if (field.rel and field.null and field.rel.to in seen_objs and 
     840                    filter(lambda f: f.column == field.column, 
     841                    field.rel.to._meta.fields)): 
    840842                update_query.clear_related(field, pk_list) 
    841843 
  • django/trunk/tests/regressiontests/queries/models.py

    r8054 r8100  
    190190class Related(models.Model): 
    191191    custom = models.ForeignKey(CustomPk) 
     192 
     193# An inter-related setup with a model subclass that has a nullable 
     194# path to another model, and a return path from that model. 
     195 
     196class Celebrity(models.Model): 
     197    name = models.CharField("Name", max_length=20) 
     198    greatest_fan = models.ForeignKey("Fan", null=True, unique=True) 
     199 
     200class TvChef(Celebrity): 
     201    pass 
     202 
     203class Fan(models.Model): 
     204    fan_of = models.ForeignKey(Celebrity) 
    192205 
    193206 
     
    8378503 
    838851 
     852Bug #7778 - Model subclasses could not be deleted if a nullable foreign key 
     853relates to a model that relates back. 
     854 
     855>>> num_celebs = Celebrity.objects.count() 
     856>>> tvc = TvChef.objects.create(name="Huey") 
     857>>> Celebrity.objects.count() == num_celebs + 1 
     858True 
     859>>> f1 = Fan.objects.create(fan_of=tvc) 
     860>>> f2 = Fan.objects.create(fan_of=tvc) 
     861>>> tvc.delete() 
     862 
     863# The parent object should have been deleted as well. 
     864>>> Celebrity.objects.count() == num_celebs 
     865True 
     866 
    839867"""} 
    840868