djangopostgresqldjango-usersdjango-3.0

Custom User Model with UUI PK cant login to admin site in Django 3.0, PostgreSQL


My customer user model looks like this :

class User(AbstractBaseUser):
    id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False, unique=True)
    email = models.EmailField(unique=True)
    is_staff = models.BooleanField(default=True)
    is_admin = models.BooleanField(default=False)
    is_superuser = models.BooleanField(default=False)
    USERNAME_FIELD = 'email'
    REQUIRED_FIELDS = []

    objects = UserManager()

    def has_perm(self, perm, obj=None):
        return self.is_admin

    def has_module_perms(self, app_label):
        return self.is_admin

    def __str__(self):
        return self.email

    class Meta:
        db_table = 'login'

When I try to login to admin after doing manage.py createsuperuser , it throws the following error :

ProgrammingError at /admin/
operator does not exist: integer = uuid
LINE 1: ...NER JOIN "login" ON ("django_admin_log"."user_id" = "login"....

I understand this has to do with PostgreSQL being strongly typed and Django not wanting to assume incoming field type. I want to go around this issue as many customer user tutorials out there specify UUID type primary key and don't seem to run into this problem when the steps ask you to login to admin and test it out.


Solution

  • I faced the same problem. I had a table named users in my database. In my case, django_admin_log table had a foreign key relation to users table. The foreign key column and the referencing column were not of the same type and that caused the problem.

    The foreign key column was INTEGER and the referenced column was UUID. Dropping and re-creating django_admin_log with the following command worked for me.

    drop table django_admin_log
    
    py manage.py migrate admin zero --fake
    py manage.py migrate