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 +
UniqueConstraint(
fields=["file"],
condition=~Q(file=""),
name="unique_non_empty_file",
)
(if you’re not using MySQL)