title: UNIQUE constraint failed: new__users_profile.user_id. Python Django
body: <blockquote>
    <p>django.db.utils.IntegrityError UNIQUE constraint failed 
    sqlite3.IntegrityError: UNIQUE constraint failed: new__users_profile.user_id
    The above exception was the direct cause of the following exception:
    new__users_profile.user_id<br></p>
  </blockquote>
  
  <p>Error occurs when I'm trying to register new user, login to an existing user and when I am trying to migrate. I've tried to delete all migrations and migrate once again but it didn't help</p>
  
  <p>models.py</p>
  
  <pre><code>from django.db import models
  from django.contrib.auth.models import User
  
  
  class Profile(models.Model):
      user = models.OneToOneField(User, on_delete=models.CASCADE, null=True)
      image = models.ImageField(default='default.jpg', upload_to='profile_pics')
  
      def __str__(self):
          return f'{self.user.username} Profile'
  
  class Media(models.Model):    
      image_name = models.CharField(max_length=50)
      image_description = models.CharField(max_length=80)
      image_image = models.ImageField(upload_to='media', default='default.jpg')
  </code></pre>
  
  <p>views</p>
  
  <pre><code>from django.shortcuts import render, redirect
  from django.contrib import messages
  from django.contrib.auth.decorators import login_required
  from django.contrib.auth.models import User
  
  from .forms import UserRegisterForm, MediaForm
  from .models import Media
  
  
  def register(request):
      if request.method == 'POST':
          form = UserRegisterForm(request.POST)
          if form.is_valid():
              form.save()
              username = form.cleaned_data.get('username')
              messages.success(request, f'Your account has been created! You are now able to log in')
              return redirect('login')
      else:
          form = UserRegisterForm()
      return render(request, 'users/register.html', {'form': form})
  
  
  @login_required
  def profile(request):
  
      if request.method == 'GET':
          media_images = Media.objects.all()
  
  
      context = {
          'media_images':media_images,
  
      }
      return render(request, 'users/profile.html', context)
  
  @login_required
  def add_media(request):
  
      if request.method == 'POST':
          form = MediaForm(request.POST, request.FILES)
          if form.is_valid():
              form.save()
              return redirect('http://127.0.0.1:8000/')
      else:
          form = MediaForm()
  
      return render(request, 'users/add_media.html', {'form':form})
  
  </code></pre>
  
  <p>forms.py</p>
  
  <pre><code>from django import forms
  from django.contrib.auth.models import User
  from django.contrib.auth.forms import UserCreationForm
  from .models import *
  
  class UserRegisterForm(UserCreationForm):
      email = forms.EmailField()
  
      class Meta:
          model = User
          fields = ['username', 'email', 'password1', 'password2']
  
  class MediaForm(forms.ModelForm):
  
      class Meta:
          model = Media
          fields = ['image_name', 'image_description', 'image_image']
  </code></pre>
  
  <pre><code>from django.db.models.signals import post_save
  from django.contrib.auth.models import User
  from django.dispatch import receiver
  from .models import Profile
  
  
  @receiver(post_save, sender=User)
  def create_profile(sender, instance, created, **kwargs):
      if created:
          Profile.objects.create(user=instance)
  
  @receiver(post_save, sender=User)
  def save_profile(sender, instance, **kwargs):
      instance.profile.save()
  </code></pre>