New Postgresql databse : column "id" is of type integer but expression is of type uuid when creating super user

Viewed 4509

I decided to work with a new database and in the same time change my custom user id field to UUID

class PersoUser(AbstractBaseUser):
id = models.UUIDField(
    primary_key=True,  default=uuid.uuid4, editable=False)
email = models.EmailField(
    verbose_name="Email Adress", max_length=200, unique=True)
username = models.CharField(
    verbose_name="username", max_length=200, unique=True)
first_name = models.CharField(verbose_name="firstname", max_length=200)
last_name = models.CharField(verbose_name="lastname", max_length=200)

date_of_birth = models.DateField(verbose_name="birthday")
is_admin = models.BooleanField(default=False)
is_active = models.BooleanField(default=True)

objects = PersoUserManager()

USERNAME_FIELD = "email"
REQUIRED_FIELDS = ["date_of_birth", "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

@property
def is_staff(self):
    "Is the user a member of staff?"
    # Simplest possible answer: All admins are staff
    return self.is_admin
class PersoUserManager(BaseUserManager):

def create_user(self, username, email, date_of_birth, password=None):
    if not username:
        raise ValueError("Users must have a username ")

    user = self.model(
        username=username,
        email=self.normalize_email(email),
        date_of_birth=date_of_birth

    )
    user.set_password(password)
    user.save(using=self._db)

    return user

def create_superuser(self, username, email, date_of_birth, password=None):
    user = self.create_user(

        username,
        email=email,
        password=password,
        date_of_birth=date_of_birth
    )
    user.is_admin = True
    user.save(using=self._db)

    return user

When attempting to create a super user threw the shell, after providing an email username psswd and date_of_birth i got the following error

django.db.utils.ProgrammingError: column "id" is of type integer but expression is of type uuid .... HINT: You will need to rewrite or cast the expression.

thanks in advance

2 Answers

That error is indicative of the database structure not matching the Django model definition. (Or, alternatively, you're forcing a numeric id somewhere in your code.) Check the table definition in your Postgres database first. If the id column is numeric, then there's something wrong with your migrations:

  • If you still have the old user-creating migration around, you will need to undo it with migrate. If you don't, you'll likely have to modify your user table manually, or, probably easier still, empty the database.
  • You will need to create the migration creating the user model in a way that has an UUID column for the primary key, and then apply it.

Worked without editing with a fresh sqlite db, for PostgreSQL ( also fresh db ) I replaced id = models.UUIDField by ID= models.UUIDField and it's now working, anyone could explained why ?

Related