python - 新的 Postgresql 数据库:列“id”是整数类型,但在创建超级用户时表达式是 uuid 类型
问题描述
我决定使用一个新数据库,同时将我的自定义用户 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
尝试创建超级用户时抛出了外壳,在提供电子邮件用户名 psswd 和 date_of_birth 后,我收到以下错误
django.db.utils.ProgrammingError: column "id" is of type integer but expression is of type uuid .... 提示:您将需要重写或强制转换表达式。
提前致谢
解决方案
该错误表明数据库结构与 Django 模型定义不匹配。(或者,或者,您id
在代码中的某处强制使用数字。)首先检查 Postgres 数据库中的表定义。如果 id 列是数字,那么您的迁移有问题:
- 如果您仍然有旧的用户创建迁移,则需要使用
migrate
. 如果你不这样做,你可能不得不手动修改你的用户表,或者,可能更容易,清空数据库。 - 您将需要以具有主键的 UUID 列的方式创建用户模型的迁移,然后应用它。
推荐阅读
- angular - Angular 8 UI observable 未触发更改检测 - ChangeDetectionStrategy 设置为默认值
- javascript - 如何使用javascript递归地将多个子属性总结为TreeTable中的父属性
- functional-programming - 转换任意一个数组
要么 (Scalaz中的序列函数) - azure - 以编程方式将应用程序设置添加到 azure 函数
- excel - VBA Excel宏从数据透视表中设置值
- arrays - 计数http响应
- ios - 有没有 Cocoapod 可以让我看到最接近的企业版本和他们的电话号码?
- html - 使用 HTML/CSS/JS 的 Web 浏览器界面?
- c - 即使输入数据相同,连续多次运行代码也会产生不同的结果
- php - 无法在 Laravel 应用程序中运行 Laravel 5 应用程序 - 未找到 Http 异常