I have got basically this model (other fields left out):
class GiftBase(models.Model):
"""
A base class for describing gift type items.
"""
uuid = models.CharField(max_length=32, default=lambda: uuid.uuid4().hex, unique=True)
When I do
./manage.py schemamigration facebookapp --auto
./manage.py migrate facebookapp
I get this output
Running migrations for facebookapp:
- Migrating forwards to 0053_auto__add_field_giftbase_uuid__add_field_purchase_uuid.
> facebookapp:0053_auto__add_field_giftbase_uuid__add_field_purchase_uuid
FATAL ERROR - The following SQL query failed: ALTER TABLE "facebookapp_giftbase" ADD COLUMN "uuid" varchar(32) NOT NULL UNIQUE DEFAULT '8de722a747974db3aab4c1fdaa618f16';
The error was: could not create unique index "facebookapp_giftbase_uuid_key"
DETAIL: Key (uuid)=(8de722a747974db3aab4c1fdaa618f16) is duplicated.
From this, I realise that the default value for uuid field is precalculated and used for each new entry and therefore it is not unique to each object. How can I get it to calculate the value for each object individually?