我有基本上這種模式(其他領域排除在外):與lambda作爲默認值和獨特的Django字段=真
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)
當我做
./manage.py schemamigration facebookapp --auto
./manage.py migrate facebookapp
我得到這個輸出
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.
從這裏我意識到,uuid字段的默認值是預先計算好的並用於每個新條目,因此它不是唯一的ea ch對象。我怎樣才能得到它分別計算每個對象的值?
能否請您分享遷移? –
正如你所看到的,默認值在你的遷移中被硬編碼。 –