Opened 4 years ago

Last modified 10 months ago

#26630 assigned Bug

Defered constraint checks flush after `post_delete` signal

Reported by: Alex Madjar Owned by: Can Sarıgöl
Component: Database layer (models, ORM) Version: master
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 4 years ago.
sample app to reproduce

Download all attachments as: .zip

Change History (7)

comment:1 Changed 4 years ago by Alex Madjar

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 Changed 4 years ago by Tim Graham

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

Changed 4 years ago by Tim Graham

Attachment: t26630.tar.gz added

sample app to reproduce

comment:3 Changed 4 years ago by Tim Graham

Triage Stage: UnreviewedAccepted

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

comment:4 Changed 11 months ago by Can Sarıgöl

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

comment:5 Changed 11 months ago by Can Sarıgöl

Has patch: set

comment:6 Changed 10 months ago by felixxm

Patch needs improvement: set
Note: See TracTickets for help on using tickets.
Back to Top