title: duplicate key value violates unique constraint in django
body: <p>I have function for creating new user in django as follows:</p>
  
  <pre><code>def initialize(username, password, email, title, firstName, lastName,  telephoneNumber, mobileNumber, smsActive, for_company_id = 1):
      sUsername = username.lower()
      if len(username) &gt; 30:
          sUsername = username[:30].lower()
      user = User.objects.create_user(sUsername, email, password, last_login=datetime.datetime.now())
      user.first_name = firstName
      user.last_name = lastName
      user.save()
      userProfile = UserProfile(user = user, title = title, telephone = telephoneNumber, mobile = mobileNumber, smsActive = smsActive)
      userProfile.code2Factor = pyotp.random_base32()
      userProfile.forCompanyId = for_company_id
      userProfile.main_updated = datetime.datetime.now()
      userProfile.save()
      return userProfile
  </code></pre>
  
  <p>Then I do it as follows:</p>
  
  <pre><code>user_profile = initialize(input.user.username, password, '', input.title, input.user.first_name, input.user.last_name, input.telephone, input.mobile, sms_active)
  user_profile.user.groups.set([Group.objects.get(id=gr.id) for gr in input.groups])
  user_profile.cultureMajor = input.cultureMajor
  user_profile.offerTax = input.offerTax if 'offerTax' in input else False
  user_profile.user.save()
  </code></pre>
  
  <p>But when I try to create new user I get error as follows:</p>
  
  <pre><code>duplicate key value violates unique constraint "auth_user_pkey" DETAIL: Key (id)=(21811) already exists.
  </code></pre>
  
  <p>The id <code>21811</code> exists in the database but the last one is <code>25530</code>.</p>
  
  <p>Why django does not use the first next number for ID?</p>
  
  <p>UPDATE</p>
  
  <ul>
  <li>User model is standard model from django. </li>
  <li><p>UserProfile is as follows:</p>
  
  <pre><code>class UserProfile(models.Model):
      forCompanyId = 1
      user = models.OneToOneField(to=User, on_delete=models.DO_NOTHING)
      title = models.CharField(max_length=10, null=True, blank=True)
      telephone = models.CharField(max_length=50, null=True, blank=True)
      mobile = models.CharField(max_length=50, null=True, blank=True)
      smsActive = models.BooleanField(default=False)
      car_by_car_notification = models.BooleanField(default=True)
      car_by_car_mail = models.BooleanField(default=True)
      daily_digest = models.BooleanField(default=True)
      offer_tax = models.BooleanField(default=False)
      cultureMajor = models.CharField(max_length=3, blank=False, default='en')
      cultureMinor = models.CharField(max_length=3, blank=True)
      modulesBlocked = models.ManyToManyField(to=Module, through='UserModuleBlocked')
      otherEmails = models.CharField(max_length=512, null=True, blank=True)
      otherContact = models.CharField(max_length=256, null=True, blank=True)
      browser = models.CharField(max_length=256, null=True, blank=True)
      picture = models.ImageField(upload_to=settings.MEDIA_ROOT, blank=True, null=True)
      code2Factor = models.CharField(max_length=256, null=True, blank=True)
      mobile2Factor = models.BooleanField(default=False)
      authenticator2Factor = models.BooleanField(default=False)
      main_user_id = models.IntegerField(null=True)
      main_updated = models.DateTimeField(_('date updated on main'), default=timezone.now)
  </code></pre></li>
  </ul>