Callable default on unique field will not generate unique values upon migrating
Using Django/DRF to create a CRUD api
I'm trying to use the RandomUUID to create a UUID from Postgres:
from django.db import models
from django.contrib.postgres.functions import RandomUUID
class Year(models.Model):
year_id = models.UUIDField(
primary_key=True, default=RandomUUID, editable=False)
When I run python manage.py makemigrations
It gives me this error:
Callable default on unique field year.year_id will not generate unique values upon migrating
What am I doing wrong?
I want Django ORM to tell Postgres to create the UUID. I don't want to use Python's uuid module.
- If you're using PostgreSQL < 13, ensure you have the pgcrypto extension correctly installed. According to the docs:
On PostgreSQL < 13, the pgcrypto extension must be installed. You can use the CryptoExtension migration operation to install it.
- Alternatively you can use Python's uuid4. In your models.py, import uuid4
from uuid import uuid4
Then in your models:
year_id = models.UUIDField(primary_key=True, default=uuid4(), editable=False)