﻿id	summary	reporter	owner	description	type	status	component	version	severity	resolution	keywords	cc	stage	has_patch	needs_docs	needs_tests	needs_better_patch	easy	ui_ux
29016	Reuse of UpdateQuery breaks delete some updates	Étienne Loks	nobody	"On a model A, when deleting a foreign key pointing to a model B, some other foreign key of the model A pointing to the same model B may be nullified.

I have isolated this behaviour on a simple project:

{{{
#!div style=""font-size: 80%""
models.py:
  {{{#!python
from django.db import models


class ChildModel(models.Model):
    name = models.CharField(max_length=200)


class ParentModel(models.Model):
    name = models.CharField(max_length=200)
    child_1 = models.ForeignKey(ChildModel, on_delete=models.SET_NULL,
                                related_name='parents_1', null=True)
    child_2 = models.ForeignKey(ChildModel, on_delete=models.SET_NULL,
                                related_name='parents_2', null=True)
  }}}
Django shell session:
  {{{#!python
from testapp.models import ParentModel, ChildModel

child_1 = ChildModel.objects.create(name=""child_1"")
child_2 = ChildModel.objects.create(name=""child_2"")
parent_1 = ParentModel.objects.create(name=""parent 1"", child_1=child_1, child_2=child_2)
parent_2 = ParentModel.objects.create(name=""parent 2"", child_1=child_2, child_2=child_1)

child_1.delete()
parent_1 = ParentModel.objects.get(pk=parent_1.pk)
parent_2 = ParentModel.objects.get(pk=parent_2.pk)
# parent_1.child_2 and parent_2.child_1 should be normaly equal to child_2 but...
parent_1.child_2 is not None and parent_2.child_1 is not None
# False is returned
  }}}
}}}

This simple project has been tested on an SQLite database. The same behaviour has been first discovered on a PostgreSQL database.

A mis-reuse of an UpdateQuery seems to be the cause of this bug.
The attached patch fixes the issue.

After search on the django bug tracker I have found another issue with the same patch attached #28099.
I have opened this new ticket because the issue seems to be more severe (I have experienced large data loss) and more general.

This issue has been found on version 1.11 and 2.0 of Django."	Bug	new	Database layer (models, ORM)	1.11	Normal				Unreviewed	0	0	0	0	0	0
