Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(account): Add unique constraint for a user's primary email
Since Django 2.2 the UniqueConstraint supports conditions. This allows enforcing only one primary email per user on the database level. The docs [1] mention that Oracle databases don't support partial indexes. I'm not sure if that means that the index will silently be ignored (just as MariaDB and MySQL do). Closes #225. [1] https://docs.djangoproject.com/en/2.2/ref/models/indexes/#django.db.models.Index.condition
- Loading branch information
Showing
4 changed files
with
62 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
# -*- coding: utf-8 -*- | ||
from __future__ import unicode_literals | ||
|
||
import django | ||
from django.db import migrations, models | ||
|
||
|
||
class Migration(migrations.Migration): | ||
|
||
dependencies = [ | ||
("account", "0002_email_max_length"), | ||
] | ||
|
||
operations = [ | ||
migrations.AddConstraint( | ||
model_name="emailaddress", | ||
constraint=models.UniqueConstraint( | ||
condition=models.Q(primary=True), | ||
fields=("user", "primary"), | ||
name="only_one_primary_for_user", | ||
), | ||
), | ||
] | ||
|
||
if django.VERSION < (2, 2): | ||
operations = [] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters