Opened 9 years ago
Last modified 3 years ago
#26630 assigned Bug
Defered constraint checks flush after `post_delete` signal
| Reported by: | Alex Madjar | Owned by: | |
|---|---|---|---|
| Component: | Database layer (models, ORM) | Version: | dev | 
| Severity: | Normal | Keywords: | |
| Cc: | Triage Stage: | Accepted | |
| Has patch: | yes | Needs documentation: | no | 
| Needs tests: | no | Patch needs improvement: | yes | 
| Easy pickings: | no | UI/UX: | no | 
Description
Repro steps:
-  Use a database that supports 
can_defer_constraint_checks(tested on postgres) -  Have a model (A) with a 
null=TrueForeignKey or OneToOneField (F) to another model (B) -  Connect a function to listen to 
post_deleteon (B) that pulls objects of type (A) from the db (using a = A.objects.get... etc ) - Try to access a.F for an object a that used to be associated with the deleted instance of B that was just deleted
 
Expectation:  a.F_id is None
Actually:  a.F_id is the id of the deleted instance and a.F raises an ObjectDoesNotExist exception
Note: doesn't repro on fields where null=False or on sqlite.  My diagnosis is that A's field isn't being properly cleared (or A objects deleted in the case of on delete CASCADE) because the constraint checks aren't being flushed until _after_ the post_delete signal fires
Attachments (1)
Change History (8)
comment:1 by , 9 years ago
comment:2 by , 9 years ago
If you could provide a test case for Django's test suite, that would be helpful.
comment:3 by , 9 years ago
| Triage Stage: | Unreviewed → Accepted | 
|---|
Seems legitimate. I attached a sample project I used to reproduce. The first test method shows the failure.
comment:5 by , 6 years ago
| Has patch: | set | 
|---|
comment:6 by , 6 years ago
| Patch needs improvement: | set | 
|---|
comment:7 by , 3 years ago
| Owner: | removed | 
|---|
Only tried on CASCADE deletes, I think it blames to this code - https://github.com/django/django/blob/master/django/db/models/deletion.py#L15