2

我有一个简单的迁移,如下所示:

class Migration(SchemaMigration):

    def forwards(self, orm):
        db.add_column('activities_newsitem', 'related_story', self.gf('django.db.models.fields.related.ForeignKey')(to=orm['projects.Story'], null=True, blank=True),keep_default=True)

不幸的是,它失败了,我不确定为什么。下面是输出。请注意,它运行添加列查询就好了。事实上,当我查看数据库时,该列是正确的。

但由于某种原因,它之后运行了一个下拉列,这就是失败的原因。为什么这样做?

DEBUG south execute "ALTER TABLE `activities_newsitem` ADD COLUMN `related_story_id` integer NULL;" with params "[]" (generic.py:145)
DEBUG south execute "ALTER TABLE `activities_newsitem` ADD CONSTRAINT `related_story_id_refs_id_3d3841088db0fdb0` FOREIGN KEY (`related_story_id`) REFERENCES `projects_story` (`id`);" with params "[]" (generic.py:145)
DEBUG south execute "CREATE INDEX `activities_newsitem_related_story_id` ON `activities_newsitem` (`related_story_id`);" with params "[]" (generic.py:145)
DEBUG south execute "ALTER TABLE `activities_newsitem` ADD COLUMN `related_story_id` integer NULL;" with params "[]" (generic.py:145)
DEBUG south execute "ALTER TABLE `activities_newsitem` ADD CONSTRAINT `related_story_id_refs_id_3d3841088db0fdb0` FOREIGN KEY (`related_story_id`) REFERENCES `projects_story` (`id`);" with params "[]" (generic.py:145)
DEBUG south execute "ALTER TABLE `activities_newsitem` DROP COLUMN `related_story_id` CASCADE;" with params "[]" (generic.py:145)
 ! Error found during real run of migration! Aborting.

 ! Since you have a database that does not support running
 ! schema-altering statements in transactions, we have had 
 ! to leave it in an interim state between migrations.

谢谢你的帮助

4

1 回答 1

1

看起来它试图运行你的命令两次。如果您查看迁移文件夹,是否有 2 个相同的迁移?如果您在没有实际迁移的情况下运行两次创建迁移命令,它可能会执行此操作。

您是否意外运行了两次命令?您可以通过打开数据库控制台并检查您的迁移是否在 south_migrationhistory 表中来检查这一点。

假设您使用的是 unixy 系统(OSX/Linux),从命令行:

mysql -u username -p password

然后

select * from south_migrationhistory;

你的移民在那里吗?

如果是这样,你的任务就完成了,你可以继续前进。如果不是,并且您的 activities_newsitem 表中有该列,则您需要伪造迁移以使您的南迁移历史保持最新。

要伪造您的迁移:

python manage.py migrate activities --fake 0001

其中 0001 是迁移编号(所有迁移文件都以数字为前缀)。

于 2012-06-19T01:08:16.267 回答