AUOI
AUOI

Reputation: 39

UNIQUE constraint failed: account_customuser.username

I have a problem with user's signup. I use Django.

When I register the first user everything is fine and the user appears in the admin panel. The problem occurs when I want to register the second user. After submiting the signup form, this error shows up:

File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/django/db/backends/utils.py", line 84, in _execute
    return self.cursor.execute(sql, params)
  File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/django/db/backends/sqlite3/base.py", line 423, in execute
    return Database.Cursor.execute(self, query, params)
sqlite3.IntegrityError: UNIQUE constraint failed: account_customuser.username

So it looks like the user will not be saved because the username (email) is not unique, but the email of the second user is different from the first one. Only superuser and first registered user appear at the admin panel.

models.py

from django.db import models
from django.contrib.auth.models import AbstractUser
from orders.models import Order


class CustomUser(AbstractUser):
    email = models.EmailField(unique=True, max_length=255, default='')
    zip_code = models.CharField(max_length=6)
    address = models.CharField(max_length=255)
    street = models.CharField(max_length=255)
    first_name = models.CharField(max_length=255)
    last_name = models.CharField(max_length=255)
    is_active = models.BooleanField(default=True)
    is_staff = models.BooleanField(default=False)
    is_admin = models.BooleanField(default=False)
    date_joined = models.DateTimeField(auto_now_add=True)
    last_login = models.DateTimeField(auto_now=True)
    email_confirmed = models.BooleanField(default=False)
    orders = models.ForeignKey(
        Order, related_name='orders', on_delete=models.CASCADE, null=True, blank=True)

    USERNAME_FIELD = 'email'
    REQUIRED_FIELDS = ['username']

    def __str__(self):
        return self.email

views.py

from django.shortcuts import render, redirect
from .forms import CreateUserForm


def signup(request):
    form = CreateUserForm()
    if request.method == 'POST':
        form = CreateUserForm(request.POST)
        if form.is_valid():
            form.save()
            return redirect('/')

    return render(request, 'views/signup.html', {'form': form})

forms.py

from django.contrib.auth.forms import UserCreationForm
from .models import CustomUser


class CreateUserForm(UserCreationForm):
    class Meta:
        model = CustomUser
        fields = ['zip_code', 'address', 'street', 'first_name',
                  'last_name', 'email', 'password1', 'password2']

Upvotes: 1

Views: 5514

Answers (1)

D Malan
D Malan

Reputation: 11414

It looks like you still have a username column in your database, but you're not creating users with usernames. So the constraint is probably failing because you'll have two users with a username of None.

If you don't need the username column, you need to add username = None to your CustomUser class and generate the migrations to remove the column.

Upvotes: 10

Related Questions