django.db.utils.IntegrityError: duplicate key value violates unique constraint "users_user_email_key"

Viewed 45

I have an issue where when I try to sign up on my app with the inputs: name, email, password, company. I get the error:

django.db.utils.IntegrityError: duplicate key value violates unique constraint "users_user_email_key" DETAIL: Key (email)=(csscsecscs@gmail.com) already exists.

My postgres database running in a container on docker desktop and I have created a migration with no issue once I added the company to the models so I am not sure what the cause of the issue is.

Models:

class User(AbstractUser):

    name = models.CharField(_("full name"), blank=True, max_length=255)
    email = models.EmailField(_('email address'), unique=True)
    company = models.CharField(_("company"), blank=True, max_length=30)

Migrations:

        migrations.CreateModel(
            name='User',
            fields=[
                ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
                ('password', models.CharField(max_length=128, verbose_name='password')),
                ('name', models.CharField(blank=True, max_length=255, verbose_name='full name')),
                ('email', models.EmailField(max_length=254, unique=True, verbose_name='email address')),
                ('company', models.CharField(blank=True, max_length=30, verbose_name='company')),

forms:

from django import forms
from django.contrib.auth.forms import UserCreationForm, UserChangeForm
from rsm_app.users.models import User


class MyUserCreationForm(UserCreationForm):
    
    field_order = ['name', 'email', 'company']
    
    class Meta(UserCreationForm):
        model = User
        fields = ('name', 'email', 'company')

signup.html:

{% extends "site_blank_base.html" %}
{% load staticfiles i18n compress %}
{% load i18n %}
{% load crispy_forms_tags %}
{% block title %}{% trans "Signup" %}{% endblock %}


{% block nav %}
{% include "nav/auth.html" %}
{% endblock nav %}


                <div class="form-wrapper">
                  <h2 class="mb-20">{% trans "Sign Up" %}</h2>                  
                  <p class="text-sm mb-25">
                    {% trans "Start using the best marketing platform of the industry today. Take a seat, the first month is on us!</p>" %}
                  </p>
                  
                  <form class="signup" id="signup_form" method="post" action="{% url 'account_signup' %}">
                    {% csrf_token %}
                    {{ form|crispy }}
                    {% if redirect_field_value %}
                      <input type="hidden" name="{{ redirect_field_name }}"
                            value="{{ redirect_field_value }}"/>
                    {% endif %}
                    <button class="btn btn-primary" type="submit">{% trans "Sign Up" %} &raquo;</button>
                  </form>
0 Answers
Related