title: (1062, "Duplicate entry '2' for key 'user_id'")
body: <p>I am trying to update a profile by cropping the image, after cropping the image it is giving me a base64 string which i have to covert it into the image and store on the server as well as its url in the database. I am getting the image on the server but unable to store its url in the database.</p>
  
  <p>models.py</p>
  
  <pre><code>class Profile(models.Model):
      user = models.OneToOneField(User, on_delete=models.CASCADE)
      image = models.FileField(default='default.jpg', 
      upload_to='profile_pics')
  
      def __str__(self):
          return self.user.username
  
      def save(self, *args, **kwargs):
            #super(Profile, self).save(*args, **kwargs)
            super().save()
            img = Image.open(self.image.path)
  
            if img.height &gt; 300 or img.width &gt; 300:
                output_size = (300,300)
                img.thumbnail(output_size)
                img.save(self.image.path)     
  </code></pre>
  
  <p>forms.py</p>
  
  <pre><code>class ProfileUpdateForm(forms.ModelForm):
      class Meta:
          model = Profile
          fields = ['image']
  </code></pre>
  
  <p>views.py</p>
  
  <pre><code>#profile view
  @login_required
  def profile(request):
      #if any changes POST data
      if request.method == 'POST':
          image_data = request.POST['image']
          format12,img = image_data.split(';base64,')
          ext = format12.split('/')[-1]
          imageObj = ContentFile(base64.b64decode(img+"=="))
          file_name = "myphoto."+ext
          #print(file_name)
          #Profile.image = data
          #Profile.save(file_name, data, save=True)
          #Profile.save()
          profile = Profile()
          #profile.user_id = request.user.id
          #profile.image= imageObj
          u_form = UserUpdateForm(request.POST, instance=request.user)
          p_form = ProfileUpdateForm(request.POST, request.FILES,instance=request.user.profile)
  
          #check if both forms are valid
          if u_form.is_valid() and p_form.is_valid():
              u_form.save() # if yes Save
              profile.image.save(file_name, imageObj)
              profile.save()
              #p_form.save() # if yes save
              messages.success(request, format('Your Profile has been updated.'))
              return redirect('profile')
      else:
          u_form = UserUpdateForm(instance=request.user)#instance will show previous data in forms
          p_form = ProfileUpdateForm()#instance will show previous data in forms
  
      # Send form data to the template
      context = {
          'u_form': u_form,
          'p_form': p_form,
      }
      return render(request, 'users/profile.html', context)
  </code></pre>
  
  <p>I am getting an error:</p>
  
  <pre><code>django.db.utils.IntegrityError: (1062, "Duplicate entry '2' for key 'user_id'")
  </code></pre>
  
  <p>Anyone expert who can help me in this?</p>