[GH-ISSUE #290] Account's email uniqueness not enforced by database #219

Open
opened 2026-02-25 23:41:39 +03:00 by kerem · 3 comments
Owner

Originally created by @cuu508 on GitHub (Sep 30, 2019).
Original GitHub issue: https://github.com/healthchecks/healthchecks/issues/290

There's a race condition when creating an user account. It is possible to create two user accounts with the same email address, which causes various problems later.

Either the email column needs a uniqueness constraint, or or the username (which already has uniqueness contraint) needs to be generated from email in a deterministic way.

Originally created by @cuu508 on GitHub (Sep 30, 2019). Original GitHub issue: https://github.com/healthchecks/healthchecks/issues/290 There's a race condition when creating an user account. It is possible to create two user accounts with the same email address, which causes various problems later. Either the email column needs a uniqueness constraint, or or the username (which already has uniqueness contraint) needs to be generated from email in a deterministic way.
Author
Owner

@cuu508 commented on GitHub (Oct 12, 2019):

I had fixed this by deriving username from email (and the username already has an uniqueness constraint, so problem solved...?)

Problem with this is that users can change their email addresses. Consider:

<!-- gh-comment-id:541301805 --> @cuu508 commented on GitHub (Oct 12, 2019): I had fixed this by deriving username from email (and the username already has an uniqueness constraint, so problem solved...?) Problem with this is that users can change their email addresses. Consider: * user@example.org creates an account. Account's username is HASH(user@example.com) * later, they change their account's email address to team@example.org. Account's username is still HASH(user@example.org) * they then try to invite user@example.org into the account. This fails with IntegrityError because HASH(user@example.org) is already taken.
Author
Owner

@cuu508 commented on GitHub (Oct 12, 2019):

Adding an explicit uniqueness constraint to the email column seems like the cleaner solution. I'm not sure if there is an idiomatic way to do that in Django for the default User model. For time being, on the hosted service, I've just manually added a constraint in the database:

ALTER TABLE auth_user ADD CONSTRAINT unique_emails UNIQUE (email);
<!-- gh-comment-id:541303752 --> @cuu508 commented on GitHub (Oct 12, 2019): Adding an explicit uniqueness constraint to the email column seems like the cleaner solution. I'm not sure if there is an idiomatic way to do that in Django for the default User model. For time being, on the hosted service, I've just manually added a constraint in the database: ALTER TABLE auth_user ADD CONSTRAINT unique_emails UNIQUE (email);
Author
Owner

@immanuelfodor commented on GitHub (Jan 3, 2020):

FYI, you can't add a unique constraint in SQLite, but can create a unique index for the same effect: https://stackoverflow.com/a/15498225

CREATE UNIQUE INDEX unique_email ON auth_user(email);

Added this manually after https://github.com/healthchecks/healthchecks/issues/306#issuecomment-570526115

<!-- gh-comment-id:570529077 --> @immanuelfodor commented on GitHub (Jan 3, 2020): FYI, you can't add a unique constraint in SQLite, but can create a unique index for the same effect: https://stackoverflow.com/a/15498225 ```sql CREATE UNIQUE INDEX unique_email ON auth_user(email); ``` Added this manually after https://github.com/healthchecks/healthchecks/issues/306#issuecomment-570526115
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
starred/healthchecks#219
No description provided.