【发布时间】:2013-01-07 12:05:48
【问题描述】:
型号:
class ListContext(models.Model):
content = models.CharField(max_length=200, blank=True)
我使用 south 来管理架构迁移。
现在我把之前的模型改成这个:
from django.contrib.contenttypes.models import ContentType
class ListContext(models.Model):
content = models.ForeignKey(ContentType, blank=True, null=True)
对于迁移:
$ python manage.py schemamigration --auto page
~ Changed field content on page.ListContext
+ Added index for ['content'] on page.ListContext
Created 0010_auto__chg_field_listcontext_content.py. You can now apply this migration with: ./manage.py migrate page
到目前为止一切都很好:
$ python manage.py migrate page
Running migrations for page:
- Migrating forwards to 0010_auto__chg_field_listcontext_content.
> page:0010_auto__chg_field_listcontext_content
FATAL ERROR - The following SQL query failed: ALTER TABLE "page_page_listcontext" ALTER
COLUMN "content_id" TYPE integer, ALTER COLUMN "content_id" DROP NOT NULL, ALTER COLUMN
"content_id" DROP DEFAULT;
The error was: column "content_id" cannot be cast to type integer
Error in migration: page:0010_auto__chg_field_listcontext_content
我可以猜到错误发生在从 string 转换为 int 的过程中,但我怎样才能避免这种情况并完成迁移?
有什么不同吗,我不在乎保留存储在表中的数据。
【问题讨论】:
-
由于您不想保留数据,因此您可以在一次迁移中删除整个模型,然后在后续迁移中使用外键重新添加它。
标签: django casting django-south database-migration