Use nulls_distinct=False instead of duplicate unique constraints

Erwan Rouchet
2025-03-31T12:36:01Z

Django 5.0 introduced [`UniqueConstraint.nulls_distinct`](https://docs.djangoproject.com/en/5.1/ref/models/constraints/#nulls-distinct), which allows settings [`NULLS [NOT] DISTINCT`](https://www.postgresql.org/docs/current/ddl-constrain...

Django 5.0 introduced UniqueConstraint.nulls_distinct, which allows settings NULLS [NOT] DISTINCT when creating a unique constraint. This causes a NULL to be treated the same as any other value in the index, so that only one NULL can be inserted into a column.

We are currently using multiple partial unique constraints to do the job of NULLS NOT DISTINCT, for example on TranscriptionEntities. Replacing those with a single unique constraint with NULLS NOT DISTINCT could lead to faster writes, and maybe allow some queries to use the unique indexes as they would now cover the whole table.

This is only supported in PostgreSQL 15 and later, so we should only do this once we are ready to drop support for PostgreSQL 14 entirely.