I want to add a new field to a PostgreSQL database.
It's a not null and unique CharField, like
dyn = models.CharField(max_length=31, null=False, unique=True)
The database already has relevant records, so it's not an option to
- delete the database
- reset the migrations
- wipe the data
- set a default static value.
How to proceed?
Edit
Tried to add a default=uuid.uuid4
dyn = models.CharField(max_length=31, null=False, unique=True, default=uuid.uuid4)
but then I get
Ensure this value has at most 31 characters (it has 36).
Edit 2
If I create a function with .hex (as found here)
def hex_uuid():
"""
The UUID as a 32-character lowercase hexadecimal string
"""
return uuid.uuid4().hex
and use it in the default
dyn = models.CharField(max_length=31, null=False, unique=True, default=hex_uuid)
I'll get
Ensure this value has at most 31 characters (it has 32).
Note: I don't want to simply get a substring of the result, like adjusting the hex_uuid()
to have return str(uuid.uuid4())[:30]
, since that'll increase the collision chances.