Opened 4 years ago
Closed 4 years ago
#33613 closed Bug (fixed)
createsuperuser does not validate usernames that use a UniqueConstraint.
| Reported by: | Lucidiot | Owned by: | Lucidiot |
|---|---|---|---|
| Component: | contrib.auth | Version: | 4.0 |
| Severity: | Normal | Keywords: | |
| Cc: | Triage Stage: | Ready for checkin | |
| Has patch: | yes | Needs documentation: | no |
| Needs tests: | no | Patch needs improvement: | no |
| Easy pickings: | no | UI/UX: | no |
Description
With a custom User model that uses a UniqueConstraint instead of unique=True, the manage.py createsuperuser command does not validate usernames at all.
class CustomUser(AbstractBaseUser): custom_username = models.CharField(max_length=255) USERNAME_FIELD = 'custom_username' class Meta: constraints = [ models.UniqueConstraint(fields=['custom_username'], name='unique_usernames'), ]
Running manage.py createsuperuser to create a user with a username that already exists then results in an IntegrityError:
IntegrityError: duplicate key value violates unique constraint "unique_usernames" DETAIL: Key (custom_username)=(foo) already exists.
Change History (4)
comment:1 by , 4 years ago
| Summary: | manage.py createsuperuser does not validate usernames that use a UniqueConstraint → createsuperuser does not validate usernames that use a UniqueConstraint. |
|---|---|
| Triage Stage: | Unreviewed → Accepted |
| Type: | Bug → Cleanup/optimization |
comment:2 by , 4 years ago
| Has patch: | set |
|---|---|
| Owner: | changed from to |
| Status: | new → assigned |
Done: PR
Should I update the documentation as well?
comment:3 by , 4 years ago
| Triage Stage: | Accepted → Ready for checkin |
|---|---|
| Type: | Cleanup/optimization → Bug |
Should I update the documentation as well?
It should be enough to change i.e. to e.g..
Note:
See TracTickets
for help on using tickets.
Thanks for this report. It's documented that "The field must be unique (i.e., have unique=True set in its definition)", however I agree that we could improve this, e.g.:
django/contrib/auth/management/commands/createsuperuser.py
field.unique:Would you like to prepare a patch? (a regression test is also required.)