0

In my Django site I created a custom user model. I am using windows authentication, so up until now, a user was able to go directly to my site. I could also create users in the admin page and edit their permissions there as well. It seems that all of a sudden I am getting the error Violation of UNIQUE KEY constraint 'UQ__accounts__AB6E616413786680'. Cannot insert duplicate key in object 'dbo.accounts_ouser'. The duplicate key value is (). The error seems like it is on the database side, because I cannot even insert a new ouser even using the server management studio.

class OUser(AbstractBaseUser, PermissionsMixin):
    email = models.EmailField(verbose_name='email address', max_length=255, unique=True)
    username = models.CharField(max_length=150, unique=True)
    is_active = models.BooleanField(default=True)
    is_staff = models.BooleanField(default=False)
    is_superuser = models.BooleanField(default=False)  # a superuser

    first_name = models.CharField(max_length=100, blank=True, default='')
    last_name = models.CharField(max_length=100, blank=True, default='')
    date_joined = models.DateField(auto_now=True)
    password = models.CharField(max_length=100)

    REQUIRED_FIELDS = []  # Email & Password are required by default.
    USERNAME_FIELD = 'username'

    def __str__(self):
        return self.username

    def has_perm(self, perm, obj=None):
        "Does the user have a specific permission?"
        # Simplest possible answer: Yes, always
        return True

    def has_module_perms(self, app_label):
        "Does the user have permissions to view the app `app_label`?"
        # Simplest possible answer: Yes, always
        return True

Another weird thing is the use of the UNIQUE KEY constraint, as the column id should be a PRIMARY KEY constraint and the ouser model does not implement any other keys. (I am not sure if that is common or not, but it seems odd to me)

What I would like is for the site to go back to how it was, where a user could visit it if they were authenticated on windows.

Does anybody have any idea why this might be?

wmoskal
  • 295
  • 1
  • 4
  • 15
  • update: If I delete an ouser, I can add another, but there seems to be a limit to the number that I can have in the database. If there is 5 in the table, I can insert, but if there is 6 in the database, it throws the error listed in the op – wmoskal Nov 07 '19 at 16:47
  • 1
    The error seems to indicate that you have users violating the unique constraint somehow, and perhaps that the unique constraint is a `NULL` value. IIRC, Microsoft violates ANSI-SQL and allows `NULL` to count as a value in a unique constraint. Can you check your database usernames and email addresses and see if you have some that are `NULL` or contain an empty string? If you take a look in the database, does the constraint `UQ__accounts__AB6E616413786680` apply to the `username` field or the `email` field? – FlipperPA Nov 08 '19 at 12:00
  • 1
    Thanks for the reply! the error was as you said, but not for null, for the empty string. The field was for the email column. Copy your comment into an answer an i'll accept it! – wmoskal Nov 08 '19 at 13:39

1 Answers1

2

The error indicates that you have users violating a unique constraint, and that the unique constraint is a NULL value or an empty string. The two fields with a unique constraint are username and email.

Through comments, we checked, and the unique constraint UQ__accounts__AB6E616413786680 actually applied to the email field. The issue was that email fields were being put in with an empty string value, ''. Cheers!

FlipperPA
  • 13,607
  • 4
  • 39
  • 71