1

i want to use uuid field as my id (primary key) but there is something wrong with it and i can't fix it ...

this is my model

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

when i try to add a new course it gave's me this error

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

my database is postgresql

please help me.

11
  • what django version do you have? Commented Feb 13, 2020 at 10:50
  • its 2.2.5 @Horatiu Jeflea Commented Feb 13, 2020 at 11:02
  • can you paste the code in which you are creating a new Course? Commented Feb 13, 2020 at 11:11
  • i dont know if i understand you right, but use django admin to add a new course @Horatiu Jeflea Commented Feb 13, 2020 at 11:35
  • got it now. But if you try to create a new Course from code, does it work? Either Course.objects.create or course.save() Commented Feb 13, 2020 at 11:37

2 Answers 2

1

Use :

id=models.CharField(primary_key=True,default=uuid.uuid4, editable=False, max_length=36)
Sign up to request clarification or add additional context in comments.

Comments

1

I find another "simpliest" solution for this issue if you wanna use uuid field as "ID" you need to start with this field as id befroe the first migration the cast missing part is because you id field dont make the change the propper way to an uuidField

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.