Opened 9 years ago

Closed 8 years ago

Last modified 3 weeks ago

#4345 closed Bug (fixed)

FileField cannot be used with unique=True

Reported by: Fabian Fagerholm <fabbe@…> Owned by: Marty Alchin
Component: Database layer (models, ORM) Version: master
Severity: Normal Keywords: fs-rf-fixed
Cc: Triage Stage: Accepted
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

The following model will result in an error in the (current) admin:

class Foo(Model):
    name = CharField(maxlength=50)
    file = FileField(upload_to="upload", unique=True)

When trying to add a Foo object in the admin, the following error occurs:

ProgrammingError at /admin/browser/documentfile/add/
can't adapt
Request Method: 	POST
Request URL: 	http://localhost:8000/admin/browser/documentfile/add/
Exception Type: 	ProgrammingError
Exception Value: 	can't adapt
Exception Location: 	/django_src/django/db/backends/util.py in execute, line 12

Removing unique=True from the file field makes it work -- but what will happen if a file with the same name is uploaded twice, attached to different objects? What will happen when either one is removed?

Change History (11)

comment:1 Changed 9 years ago by Fabian Fagerholm <fabbe@…>

Component: Database wrapperDocumentation
Needs documentation: unset
Needs tests: unset
Owner: changed from Adrian Holovaty to Jacob
Patch needs improvement: unset

Hmm...

Apparently, what happens is this:
If the file already exists, the new file will be named <base name><_>.<suffix>, where <_> is the number of underscores needed to make the name unique. So, for example, when a file named foo.png is uploaded multiple times, the second file will be named foo_.png, the third will be foo__.png and so on.

This should probably be documented, though. I'm not sure whether this renaming happens in the admin code or in the model code.

comment:2 Changed 9 years ago by Chris Beaven

Triage Stage: UnreviewedAccepted

Well it shouldn't be erroring, so let's accept this as a valid bug.

Perhaps FileField just shouldn't accept the unique=True argument.

comment:3 Changed 9 years ago by Fabian Fagerholm <fabbe@…>

Just as a side note: http://www.djangoproject.com/documentation/db-api/ actually documents this:

save_FOO_file(filename, raw_contents)

"For every FileField, the object will have a save_FOO_file() method, where FOO is the name of the field. This saves the given file to the filesystem, using the given filename. If a file with the given filename already exists, Django adds an underscore to the end of the filename (but before the extension) until the filename is available."

Could be mentioned in the FileField docs as well, though.

comment:4 Changed 9 years ago by Jacob

Component: DocumentationDatabase wrapper
Owner: changed from Jacob to Adrian Holovaty

This isn't a doc bug; it's a model API bug. Actually, though, I'm not clear on what a "unique" file would be -- do we have do examine each uploaded file against all the others? That's crazy talk, I think... Perhaps just disallowing unique on FileFields would do the trick.

comment:5 Changed 9 years ago by Marty Alchin

Keywords: fs-rf added

comment:6 Changed 9 years ago by Marty Alchin

Keywords: fs-rf-fixed added; fs-rf removed

comment:7 Changed 8 years ago by Marty Alchin

milestone: 1.0 beta

comment:8 Changed 8 years ago by Marty Alchin

Owner: changed from nobody to Marty Alchin
Status: newassigned

comment:9 Changed 8 years ago by Jacob

Resolution: fixed
Status: assignedclosed

(In [8244]) File storage refactoring, adding far more flexibility to Django's file handling. The new files.txt document has details of the new features.

This is a backwards-incompatible change; consult BackwardsIncompatibleChanges for details.

Fixes #3567, #3621, #4345, #5361, #5655, #7415.

Many thanks to Marty Alchin who did the vast majority of this work.

comment:11 Changed 5 years ago by Jacob

milestone: 1.0 beta

Milestone 1.0 beta deleted

comment:12 Changed 3 weeks ago by Tim Graham

Easy pickings: unset
Severity: Normal
Type: Bug
UI/UX: unset

#27188 is a follow up ticket that argues to allow using unique=True with FileField.

Note: See TracTickets for help on using tickets.
Back to Top