From c74d1bd0ca665b6e3e21ff8e9138e9ee3a133956 Mon Sep 17 00:00:00 2001 From: Matteo Vitali <trottomv@gmail.com> Date: Sat, 18 Nov 2023 09:40:33 +0100 Subject: [PATCH] Fix #5 Prevent MigrationAutoDetector removes the user unique email constraint --- src/unique_user_email/apps.py | 2 +- tests/tests.py | 8 ++++++++ 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/src/unique_user_email/apps.py b/src/unique_user_email/apps.py index d407dfb..9681eac 100644 --- a/src/unique_user_email/apps.py +++ b/src/unique_user_email/apps.py @@ -23,4 +23,4 @@ def ready(self): ] User._meta.constraints = User.Meta.constraints # ... as long as original_attrs is not updated. - # User._meta.original_attrs["constraints"] = User.Meta.constraints + User._meta.original_attrs["constraints"] = User.Meta.constraints diff --git a/tests/tests.py b/tests/tests.py index fd87b87..daf7fba 100644 --- a/tests/tests.py +++ b/tests/tests.py @@ -6,6 +6,7 @@ from django.test import TestCase from unique_user_email.backend import EmailBackend from unique_user_email.forms import AuthenticationForm +from django.db.models.constraints import UniqueConstraint class UniqueEmailTestCase(TestCase): @@ -55,6 +56,13 @@ def test_model_save_disallows_duplicate_emails(self): ): user2.save() + def test_user_constraints(self): + self.assertIsInstance(User._meta.constraints[0], UniqueConstraint) + self.assertEqual("unique_user_email", User._meta.constraints[0].name) + self.assertEqual( + "unique_user_email", + User._meta.original_attrs.get("constraints")[0].name + ) class EmailBackendTests(TestCase): def test_none_for_username_logins(self):