﻿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
9308	Django fails to set nullable foreign key field to NULL before deleting the referenced record	egenix_viktor	nobody	"Unit test: `modeltests/delete`

Test case:

{{{
# Since E.f is nullable, we should delete F first (after nulling out
# the E.f field), then E.

>>> o = CollectedObjects()
>>> e1._collect_sub_objects(o)
>>> o.keys()
[<class 'modeltests.delete.models.F'>, <class 'modeltests.delete.models.E'>]

>>> e1.delete()
}}}

It should work as described in the associated comment, since f_id is a nullable column of the delete_e table:

{{{
CREATE TABLE [delete_e] (
    [id] int IDENTITY (1, 1) NOT NULL PRIMARY KEY,
    [f_id] int NULL
)
}}}

Django should set the f_id field in the E record to NULL before trying to delete the F record, but it does not do so. Instead it tries to delete the F record first, which fails the foreign key constraint check associated to the f_id field of the delete_e table (E record).

It does not work on databases where deferred constrain checking (checking constraints only COMMIT time) is not an option. For example I found this behavior while running the unit tests through pyodbc (third party, I know) and MSSQL. (Relying on such deferred constraint checking is not a good practice at all.)"		closed	Database layer (models, ORM)	1.0		fixed	delete record foreign key constraint check NULL nullable field	bthomas@… liangent@… jdunck@…	Accepted	1	0	0	0	0	0
