Skip to content

[Bug]: IntegrityError when creating new users via Django Admin (duplicate key value violates unique constraint on email) #1267

Description

@e-reiter

Bug Description

Problem Description
When attempting to add a new user through the Django Admin interface at /admin/users/customuser/add/, an IntegrityError is thrown:


django.db.utils.IntegrityError: duplicate key value violates unique constraint "users_customuser_email_6445acef_uniq"
DETAIL: Key (email)=() already exists.

Steps to Reproduce

  1. Log into the Django Admin dashboard (/admin).
  2. Navigate to Users -> Add user (/admin/users/customuser/add/).
  3. Fill in a username and password and click Save.
  4. The form crashes with an IntegrityError (HTTP 500).

Expected Behavior

User should be created.

Screenshots / Logs

No response

Platform

Proxmox

Install Method

Quick Install Script

AdventureLog Version

v0.13.0

Reverse Proxy

none

Docker Compose / Relevant Variables (Optional)

Additional Context

  1. In users/models.py, CustomUser defines the email field with a unique constraint (email = models.EmailField(unique=True)).
  2. In users/admin.py, CustomUserAdmin inherits from Django's default django.contrib.auth.admin.UserAdmin without overriding add_form or add_fieldsets.
  3. Django's default AdminUserCreationForm only includes username and password fields. When submitting the creation form, email defaults to an empty string ("").
  4. In PostgreSQL, empty strings are treated as distinct values subject to unique constraints (unlike NULL values in standard SQL unique indexes). If one user already exists with an empty string (e.g. an initial superuser created without an email prompt), any subsequent user creation via the admin form attempts to insert email="", causing a uniqueness collision.

Proposed fix:

  1. Update users/admin.py
    Define a CustomUserCreationForm derived from django.contrib.auth.forms.AdminUserCreationForm that explicitly includes the email field, and assign it to CustomUserAdmin.add_form and CustomUserAdmin.add_fieldsets.
from django.contrib import admin
from django.contrib.auth.admin import UserAdmin, AdminUserCreationForm
from .models import CustomUser

class CustomUserCreationForm(AdminUserCreationForm):
    class Meta(AdminUserCreationForm.Meta):
        model = CustomUser
        fields = ("username", "email")

@admin.register(CustomUser)
class CustomUserAdmin(UserAdmin):
    model = CustomUser
    add_form = CustomUserCreationForm

    # Include email in the add view fieldsets
    add_fieldsets = (
        (
            None,
            {
                "classes": ("wide",),
                "fields": ("username", "email", "usable_password", "password1", "password2"),
            },
        ),
    )

Confirmation

  • I searched existing issues and confirmed this is not a duplicate.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    backlogIdea or request that has not been reviewed yet.bugSomething isn't workingdeploymentIssues related to deploying AdventureLog

    Projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions