Reputation: 33615
I've changed my model PK from this:
id = models.CharField(primary_key=True, unique=True, default=make_key, max_length=32)
to this
id = models.UUIDField(primary_key=True, unique=True, default=TimeUUID, editable=False)
Where TimeUUID is this function. However, I get an error when running migrate below, why?
The DB has no data in I cleared all rows. The function generates UUIDs that look like this:
00332873-d693-da82-720e-27dff1cf2a9b
00332873-d693-db36-dd92-eac7c082f13c
00332873-d693-dc0a-aef8-9fb752a3fb6c
i.e. [32 bits timestamp high]-[16 bits timestamp mid]-[16 bits timestamp low]-[16 bits random]-[48 bits random]
The DB is Postgres.
Error:
File "/Users/user/Documents/app/env/lib/python3.4/site-packages/django/db/backends/utils.py", line 64, in execute
return self.cursor.execute(sql, params)
django.db.utils.ProgrammingError: can't adapt type 'TimeUUID'
Could it be that the Django UUID field does not accept UUIDs with bits in a different order?
Also tried:
def get_UUID():
return TimeUUID()
Then
id = models.UUIDField(primary_key=True, unique=True, default=get_UUID, editable=False)
Which still gives the same error.
Upvotes: 0
Views: 948
Reputation: 599580
TimeUUID is a class. Calling it returns an instance of that class. Django has no idea what to do with that value, as there is no clue how it could be stored as a value in the database.
In order to use it, you'd need to convert it to a string. You could do that in a lambda:
id = models.UUIDField(
primary_key=True, unique=True, default=lambda: str(TimeUUID()), editable=False)
Upvotes: 1