Adding a unique together constraint concurrently

Adrienne Domingus
3 min readJan 1, 2018

Adding a unique_together constraint to a database model in Django is easy. Add it to the Meta class on your model, makemigrations, migrate, and voilà. This works well if you’re creating a new table, or if you have a low traffic application/low-volume table that won’t take long to migrate. However, in order to add a uniqueness constraint, an index must first be created (so that lookups to confirm if a duplicate already exists can be done quickly). This can be problematic. According to the Postgres docs:

…a standard index build locks out writes (but…