Search code examples
pythondjangoormdjango-nonrel

Why doesn't django's create_user method validate uniqueness?


from django.contrib.auth.models import User

a = User.objects.create_user("1","2","3")
b = User.objects.create_user("1","2","3")

no errors yet...

a.validate_unique()
ValidationError: {'username': [u'User with this Username already exists.']}

I would expect to receive the validation error when b was being saved as part of create_user.

Looking at the Django Model Instance Reference, the 'what happens when you save' section does not describe saving at any point.

Is this an intentional design decision or a side-effect due to using django-nonrel? I would have expected a relational database to return an IntegrityError or something similar.


Solution

  • You might be using SQLite, which does not support unique key constraints.

    create_user assumes you've done all the needed checking, usually in a form's validation functions. It gives you quite enough rope to hang yourself with.