# FileField null=True unique=True

**URL:** <https://forum.djangoproject.com/t/filefield-null-true-unique-true/43931>\
**Category:** Documentation\
**Created:** [January 15, 2026, 9:42am UTC](https://forum.djangoproject.com/t/filefield-null-true-unique-true/43931 "2026-01-15T09:42:43Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![bery](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/bery/32/21116_2.png) [@bery](https://forum.djangoproject.com/u/bery)\
**Post date:** [January 15, 2026, 9:42am UTC](https://forum.djangoproject.com/t/filefield-null-true-unique-true/43931/1 "2026-01-15T09:42:43Z")

</div>

Seems like `FileField` / `ImageField` cannot have `null=True` + `unique=True`  
I’m testing this on sqlite and I get `IntegrityError: UNIQUE constraint failed` for multiple rows without a file.  
`FieldFile` seems to always save empty string to the db, even when I do `foo.objects.create(file=None)`

Shouldn’t this behavior be mentioned in the docs? I can’t find it anywhere

I guess the simplest solution then is `null=False` + `blank=True` +

```python
UniqueConstraint(
    fields=["file"],
    condition=~Q(file=""),
    name="unique_non_empty_file",
)

```

(if you’re not using MySQL)

---

<div class="post-metadata">

**Author:** ![philgyford](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/philgyford/32/14295_2.png) [@philgyford](https://forum.djangoproject.com/u/philgyford)\
**Post date:** [January 15, 2026, 10:35am UTC](https://forum.djangoproject.com/t/filefield-null-true-unique-true/43931/2 "2026-01-15T10:35:45Z")

</div>

There was [a discussion about this](https://forum.djangoproject.com/t/ticket-10244-filefield-cant-be-set-to-null-in-the-database/37137/6) just over a year ago, about [a bug](https://code.djangoproject.com/ticket/10244) that’s been unresolved for 17 years now.

On the plus side, at least you’re not alone 🙂

---

<div class="post-metadata">

**Author:** ![bery](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/bery/32/21116_2.png) [@bery](https://forum.djangoproject.com/u/bery)\
**Post date:** [January 15, 2026, 10:54am UTC](https://forum.djangoproject.com/t/filefield-null-true-unique-true/43931/3 "2026-01-15T10:54:14Z")

</div>

Aha, I didn’t find it, thanks!

---

<div class="post-metadata">

**Author:** ![George8192-ia](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/george8192-ia/32/31221_2.png) [@George8192-ia](https://forum.djangoproject.com/u/George8192-ia)\
**Post date:** [January 15, 2026, 11:48am UTC](https://forum.djangoproject.com/t/filefield-null-true-unique-true/43931/4 "2026-01-15T11:48:29Z")

</div>

This behavior is a common gotcha with Django’s FileField and ImageField. Since empty values are stored as empty strings instead of NULL, the unique constraint conflict on SQLite makes sense. A conditional UniqueConstraint is usually the cleanest workaround.
