【问题标题】:UUIDField in Django (error:operator does not exist: integer = uuid)Django中的UUIDField(错误:运算符不存在:整数= uuid)
【发布时间】:2020-05-29 01:00:49
【问题描述】:

我想使用 uuid 字段作为我的 id(主键),但它有问题,我无法修复它...

这是我的模特

class Course(models.Model):

    id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
    title = models.CharField(max_length=90)
    description = models.TextField()
    author = models.CharField(max_length=60)
    image = models.ImageField(upload_to='courses/images')
    intro_video = models.FileField(upload_to='courses/videos')
    free = models.BooleanField(default=False)
    price = models.CharField(max_length=60, blank=True)
    completed = models.BooleanField(default=False)
    duration = models.CharField(max_length=30, blank=True)
    created_at = models.DateTimeField(auto_now_add=True)

    def __str__(self):
        return self.title

当我尝试添加新课程时,它给了我这个错误

ProgrammingError at /admin/courses/course/add/
operator does not exist: integer = uuid
LINE 1: ..., "created_at" = NULL WHERE "courses_course"."id" = 'a130811...
                                                             ^
HINT:  No operator matches the given name and argument types. You might need to add explicit type casts.

Exception Type: ProgrammingError
Exception Value: operator does not exist: integer = uuid

我的数据库是postgresql

请帮帮我。

【问题讨论】:

  • 你有什么 django 版本?
  • 它的 2.2.5 @Horatiu Jeflea
  • 您可以粘贴您正在创建新课程的代码吗?
  • 我不知道我是否理解正确,但使用 django admin 添加新课程@Horatiu Jeflea
  • 现在知道了。但是,如果您尝试从代码创建新课程,它会起作用吗? Course.objects.create 或 course.save()

标签: django postgresql django-models uuid


【解决方案1】:

我为这个问题找到了另一个“最简单”的解决方案,如果你想使用 uuid 字段作为“ID”,你需要在第一次迁移之前将此字段作为 id到 uuidField 的正确方法

【讨论】:

    【解决方案2】:

    使用:

    id=models.CharField(primary_key=True,default=uuid.uuid4, editable=False, max_length=36)
    

    【讨论】:

      猜你喜欢
      • 2022-10-15
      • 2018-08-03
      • 2015-12-03
      • 1970-01-01
      • 1970-01-01
      • 2017-09-21
      • 1970-01-01
      • 2016-11-13
      • 2020-02-05
      相关资源
      最近更新 更多