#6456 closed (fixed)
FileField deletes files even when model instance is deleted in a transaction that is rolled back
Reported by: | durdinator | Owned by: | nobody |
---|---|---|---|
Component: | File uploads/storage | Version: | dev |
Severity: | Keywords: | ||
Cc: | Triage Stage: | Accepted | |
Has patch: | no | Needs documentation: | yes |
Needs tests: | no | Patch needs improvement: | no |
Easy pickings: | no | UI/UX: | no |
Description
I've got a model with a few FileFields. I've got code resembling this:
class SomeObject(models.Model): ... class RelatedFile(models.Model): filename = models.FileField(...) owner = models.ForeignKey(SomeObject, related_name='related_files') @commit_on_success def update_files(request, id): obj = get_object_or_404(SomeObject, pk=id) obj.related_files.all().delete() ... more code - which depending on external conditions can raise an exception ...
Since the FileField hooks the RelatedFile's post_delete signal, it deletes the original files when the delete() method is called. Then if the transaction is rolled back, the rows in the database, not being deleted after all, point to nonexistent files.
Change History (9)
comment:1 Changed 15 years ago by
Triage Stage: | Unreviewed → Accepted |
---|
comment:2 Changed 14 years ago by
Component: | Core framework → File uploads/storage |
---|
comment:3 Changed 12 years ago by
Needs documentation: | set |
---|
This is a severe dataloss bug that has bitten me badly recently. Suggesting raising the priority of this, although it's too late for 1.3 now. I'll try to at least provide a test case and a doc patch as soon as i find some time.
comment:4 Changed 12 years ago by
milestone: | → 1.3 |
---|
It's not too late at all. 1.3 final won't be released until mid January at the earliest.
I think this adds weight to Malcolm's argument at DjangoCon that FileField shouldn't *ever* delete a file, and the cleanup task should be left to an external process.
comment:6 Changed 12 years ago by
For what it's worth, as someone who has been bitten by this bug, I absolutely agree that FileField should never delete files in the filesystem, especially if we cannot guarantee transactional integrity (which we probably cannot given the mechanisms we currently have).
comment:7 Changed 12 years ago by
Resolution: | → fixed |
---|---|
Status: | new → closed |
right now there's no signal for commit/rollback, and I guess we'd need that to make sure the files aren't deleted yet. See #5415 for the implementation of something similar.