#35657 closed Bug (fixed)
Specifying db_default on FileField causes crash on instance save
| Reported by: | David Sanders | Owned by: | Sarah Boyce |
|---|---|---|---|
| Component: | Database layer (models, ORM) | Version: | 5.0 |
| Severity: | Release blocker | Keywords: | db_default |
| Cc: | Triage Stage: | Ready for checkin | |
| Has patch: | yes | Needs documentation: | no |
| Needs tests: | no | Patch needs improvement: | no |
| Easy pickings: | no | UI/UX: | no |
Description
I guess there's the existential question: "Does db_default make sense with FileField/ImageField?" Perhaps, as long as there's a file at the path defined by the db_default expression? 🤔
In any case when defined with a FileField, the presence of DatabaseDefault as the field's value causes a crash:
Given the model:
class Foo(models.Model):
bar = models.FileField(db_default="path/to/file.txt")
We get AttributeError exceptions when attempting to create an instance:
>>> Foo.objects.create()
Traceback (most recent call last):
File "<console>", line 1, in <module>
File "/path/to/django/django/db/models/manager.py", line 87, in manager_method
return getattr(self.get_queryset(), name)(*args, **kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/path/to/django/django/db/models/query.py", line 660, in create
obj.save(force_insert=True, using=self.db)
File "/path/to/django/django/db/models/base.py", line 891, in save
self.save_base(
File "/path/to/django/django/db/models/base.py", line 997, in save_base
updated = self._save_table(
^^^^^^^^^^^^^^^^^
File "/path/to/django/django/db/models/base.py", line 1160, in _save_table
results = self._do_insert(
^^^^^^^^^^^^^^^^
File "/path/to/django/django/db/models/base.py", line 1201, in _do_insert
return manager._insert(
^^^^^^^^^^^^^^^^
File "/path/to/django/django/db/models/manager.py", line 87, in manager_method
return getattr(self.get_queryset(), name)(*args, **kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/path/to/django/django/db/models/query.py", line 1828, in _insert
return query.get_compiler(using=using).execute_sql(returning_fields)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/path/to/django/django/db/models/sql/compiler.py", line 1847, in execute_sql
for sql, params in self.as_sql():
^^^^^^^^^^^^^
File "/path/to/django/django/db/models/sql/compiler.py", line 1770, in as_sql
value_rows = [
^
File "/path/to/django/django/db/models/sql/compiler.py", line 1771, in <listcomp>
[
File "/path/to/django/django/db/models/sql/compiler.py", line 1772, in <listcomp>
self.prepare_value(field, self.pre_save_val(field, obj))
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/path/to/django/django/db/models/sql/compiler.py", line 1720, in pre_save_val
return field.pre_save(obj, add=True)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/path/to/django/django/db/models/fields/files.py", line 320, in pre_save
if file.name is None and file._file is not None:
^^^^^^^^^
AttributeError: 'DatabaseDefault' object has no attribute 'name'
Change History (12)
comment:1 by , 15 months ago
| Summary: | Specifying db_default on FileField crashes → Specifying db_default on FileField causes crash on instance save |
|---|
comment:2 by , 15 months ago
| Has patch: | set |
|---|---|
| Owner: | set to |
| Severity: | Normal → Release blocker |
| Status: | new → assigned |
| Triage Stage: | Unreviewed → Accepted |
comment:3 by , 15 months ago
| Patch needs improvement: | set |
|---|
comment:4 by , 15 months ago
| Has patch: | unset |
|---|---|
| Owner: | removed |
| Patch needs improvement: | unset |
| Status: | assigned → new |
follow-up: 6 comment:5 by , 15 months ago
Hi, I tested this issue and it indeed exists. It can be resolved by initiating self.name to None in init, and that's it
I would send a PR to address this alongside a test, if you are ok.
comment:6 by , 15 months ago
Replying to Mohammad Salehi:
Hi, I tested this issue and it indeed exists. It can be resolved by initiating self.name to None in init, and that's it
I would send a PR to address this alongside a test, if you are ok.
That doesn't sound right
I've opened a PR
comment:7 by , 15 months ago
| Owner: | set to |
|---|---|
| Status: | new → assigned |
comment:8 by , 15 months ago
| Has patch: | set |
|---|
comment:9 by , 15 months ago
| Triage Stage: | Accepted → Ready for checkin |
|---|
I feel like the behavior should mimic default here - accepting