python - 新建 Postgresql 数据库 : column "id" is of typ

我决定使用一个新数据库,同时将我的自定义用户 ID 字段更改为 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

当尝试创建 super 用户时抛出 shell,在提供电子邮件用户名 psswd 和 date_of_birth 后我收到以下错误

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.

提前致谢

最佳答案

该错误表明数据库结构与 Django 模型定义不匹配。 (或者,您在代码中的某处强制使用数字 id。)首先检查 Postgres 数据库中的表定义。如果 id 列是数字,那么你的迁移有问题:

  • 如果您仍然有旧的用户创建迁移,您将需要使用 migrate 撤消它。如果不这样做,您可能必须手动修改用户表,或者更简单的方法是清空数据库。
  • 您需要创建迁移,以主键具有 UUID 列的方式创建用户模型,然后应用它。

关于python - 新建 Postgresql 数据库 : column "id" is of type integer but expression is of type uuid when creating super user,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62684133/

相关文章:

python - 比较两个列表,如果相等则替换第三个列表中的值

python - 如何正确创建具有所需大小和分辨率(PNG、JPG、JPEG)的新图像?

mongodb - 如果输入数组的元素至少匹配一个,则获取数组的大小

java - Bootstrap 类路径未与 -source 8 一起设置

typescript - TypeScript 中任意数量类型的非析取联合

python - 如何提高 aiohttp 爬虫速度?

azure - 如何使用 terraform 创建 azure 事件网格系统主题?

reactjs - fork 项目返回未经授权的 token

javascript - 在 Chrome 中将下/上填充应用于具有最小值和最大值的 "input

jsf - Primefaces Dynaform 奇怪的行为与 f :validateLength