How is it possible for django to go through DoesNotExist and then raise "Duplicate entry"?
<p>I received the following django error</p>

<pre><code>File "./project/auth_backend.py", line 31, in authenticate
user.save()
...
IntegrityError: (1062, "Duplicate entry 'user_name' for key 'username'") 
</code></pre>

<p>The said file contains the following code (which is based on <a href="https://docs.djangoproject.com/en/1.8/topics/auth/customizing/#writing-an-authentication-backend" rel="nofollow">https://docs.djangoproject.com/en/1.8/topics/auth/customizing/#writing-an-authentication-backend</a>)</p>

<pre><code>try:
    user = User.objects.get(username=username)
except User.DoesNotExist:
    user = User(username=username, password=password)
...
user.save()
</code></pre>

<p>It seems that somehow it raised User.DoesNotExist even when it actually does exist. My database is MySQL.</p>

<p>I've seen others with a similar problem but unlike them, I'm not using any database caching.</p>