Opened 9 years ago

Last modified 21 months 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:

  1. Use a database that supports can_defer_constraint_checks (tested on postgres)
  2. Have a model (A) with a null=True ForeignKey or OneToOneField (F) to another model (B)
  3. Connect a function to listen to post_delete on (B) that pulls objects of type (A) from the db (using a = A.objects.get... etc )
  4. 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)

t26630.tar.gz (1013 bytes ) - added by Tim Graham 9 years ago.
sample app to reproduce

Download all attachments as: .zip

Change History (8)

comment:1 by Alex Madjar, 9 years ago

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

comment:2 by Tim Graham, 9 years ago

If you could provide a test case for Django's test suite, that would be helpful.

by Tim Graham, 9 years ago

Attachment: t26630.tar.gz added

sample app to reproduce

comment:3 by Tim Graham, 9 years ago

Triage Stage: UnreviewedAccepted

Seems legitimate. I attached a sample project I used to reproduce. The first test method shows the failure.

comment:4 by Can Sarıgöl, 5 years ago

Owner: changed from nobody to Can Sarıgöl
Status: newassigned
Version: 1.9master

comment:5 by Can Sarıgöl, 5 years ago

Has patch: set

comment:6 by Mariusz Felisiak, 5 years ago

Patch needs improvement: set

comment:7 by Can Sarıgöl, 21 months ago

Owner: Can Sarıgöl removed
Note: See TracTickets for help on using tickets.
Back to Top