Skip to content

Commit

Permalink
Fixed #9308 -- Corrected the updated of nullable foreign key fields w…
Browse files Browse the repository at this point in the history
…hen deleting objects. Thanks to Bob Thomas for the fix, and markshep for the improvements on the test case.

git-svn-id: http://code.djangoproject.com/svn/django/trunk@10822 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information
freakboy3742 committed May 19, 2009
1 parent ae95edf commit b9b9ca3
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 5 deletions.
6 changes: 3 additions & 3 deletions django/db/models/query.py
Expand Up @@ -615,7 +615,7 @@ def only(self, *fields):
clone = self._clone()
clone.query.add_immediate_loading(fields)
return clone

###################################
# PUBLIC INTROSPECTION ATTRIBUTES #
###################################
Expand All @@ -632,7 +632,7 @@ def ordered(self):
else:
return False
ordered = property(ordered)

###################
# PRIVATE METHODS #
###################
Expand Down Expand Up @@ -1007,7 +1007,7 @@ def delete_objects(seen_objs):
update_query = sql.UpdateQuery(cls, connection)
for field, model in cls._meta.get_fields_with_model():
if (field.rel and field.null and field.rel.to in seen_objs and
filter(lambda f: f.column == field.column,
filter(lambda f: f.column == field.rel.get_related_field().column,
field.rel.to._meta.fields)):
if model:
sql.UpdateQuery(model, connection).clear_related(field,
Expand Down
16 changes: 14 additions & 2 deletions tests/modeltests/delete/models.py
Expand Up @@ -30,7 +30,7 @@ class D(DefaultRepr, models.Model):
# D -> A

# So, we must delete Ds first of all, then Cs then Bs then As.
# However, if we start at As, we might find Bs first (in which
# However, if we start at As, we might find Bs first (in which
# case things will be nice), or find Ds first.

# Some mutually dependent models, but nullable
Expand Down Expand Up @@ -96,7 +96,7 @@ class F(DefaultRepr, models.Model):
>>> def clear_rel_obj_caches(models):
... for m in models:
... if hasattr(m._meta, '_related_objects_cache'):
... if hasattr(m._meta, '_related_objects_cache'):
... del m._meta._related_objects_cache
# Nice order
Expand Down Expand Up @@ -168,7 +168,16 @@ class F(DefaultRepr, models.Model):
>>> o.keys()
[<class 'modeltests.delete.models.F'>, <class 'modeltests.delete.models.E'>]
# temporarily replace the UpdateQuery class to verify that E.f is actually nulled out first
>>> import django.db.models.sql
>>> class LoggingUpdateQuery(django.db.models.sql.UpdateQuery):
... def clear_related(self, related_field, pk_list):
... print "CLEARING FIELD",related_field.name
... return super(LoggingUpdateQuery, self).clear_related(related_field, pk_list)
>>> original_class = django.db.models.sql.UpdateQuery
>>> django.db.models.sql.UpdateQuery = LoggingUpdateQuery
>>> e1.delete()
CLEARING FIELD f
>>> e2 = E()
>>> e2.save()
Expand All @@ -185,6 +194,9 @@ class F(DefaultRepr, models.Model):
[<class 'modeltests.delete.models.F'>, <class 'modeltests.delete.models.E'>]
>>> f2.delete()
CLEARING FIELD f
# Put this back to normal
>>> django.db.models.sql.UpdateQuery = original_class
"""
}

0 comments on commit b9b9ca3

Please sign in to comment.