Commit b9b9ca33 authored by Russell Keith-Magee's avatar Russell Keith-Magee
Browse files

Fixed #9308 -- Corrected the updated of nullable foreign key fields when...

Fixed #9308 -- Corrected the updated of nullable foreign key fields when 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
parent ae95edf9
Loading
Loading
Loading
Loading
+3 −3
Original line number Diff line number Diff line
@@ -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,
+14 −2
Original line number Diff line number Diff line
@@ -168,7 +168,16 @@ True
>>> 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()
@@ -185,6 +194,9 @@ True
[<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
"""
}