Skip to content

Commit

Permalink
Use UniqueConstraint instead of unique_together for membership
Browse files Browse the repository at this point in the history
  • Loading branch information
nikochiko committed Jul 23, 2024
1 parent 513acfd commit 3c34625
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
# Generated by Django 4.2.7 on 2024-07-23 11:45

from django.db import migrations, models
import django.db.models.deletion


class Migration(migrations.Migration):

dependencies = [
('app_users', '0019_alter_appusertransaction_reason'),
('orgs', '0002_alter_org_unique_together_and_more'),
]

operations = [
migrations.RemoveConstraint(
model_name='org',
name='unique_domain_name_when_not_deleted',
),
migrations.AlterUniqueTogether(
name='orgmembership',
unique_together=set(),
),
migrations.AlterField(
model_name='orginvitation',
name='status_changed_by',
field=models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.SET_NULL, related_name='received_invitations', to='app_users.appuser'),
),
migrations.AddConstraint(
model_name='org',
constraint=models.UniqueConstraint(condition=models.Q(('deleted__isnull', True)), fields=('domain_name',), name='unique_domain_name_when_not_deleted', violation_error_message='This domain name is already in use by another team. Contact Gooey.AI Support <[email protected]> if you think this is a mistake.'),
),
migrations.AddConstraint(
model_name='orgmembership',
constraint=models.UniqueConstraint(condition=models.Q(('deleted__isnull', True)), fields=('org', 'user'), name='unique_org_user'),
),
]
8 changes: 7 additions & 1 deletion orgs/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,13 @@ class OrgMembership(SafeDeleteModel):
objects = SafeDeleteManager()

class Meta:
unique_together = ("org", "user", "deleted")
constraints = [
models.UniqueConstraint(
fields=["org", "user"],
condition=Q(deleted__isnull=True),
name="unique_org_user",
)
]

def __str__(self):
return f"{self.get_role_display()} - {self.user} ({self.org})"
Expand Down

0 comments on commit 3c34625

Please sign in to comment.