我有一个简单的迁移,如下所示:
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)
不幸的是,它失败了,我不知道为什么。以下是输出。请注意,它运行添加列查询就好了。事实上,当我查看数据库时,列就是正确的。
但出于某种原因,它之后会运行一个drop column,那就是失败了。为什么这样做?
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.
感谢您的帮助
答案 0 :(得分:1)
看起来它试图两次运行你的命令。如果查看迁移文件夹,是否有2个相同的迁移?如果您在没有实际迁移的情况下运行两次create migration命令,则可能会执行此操作。
你是不是意外地执行了两次命令? 您可以通过打开数据库控制台并检查迁移是否在south_migrationhistory表中来检查这一点。
假设您使用的是unixy系统(OSX / Linux),请从命令行:
mysql -u username -p password
然后
select * from south_migrationhistory;
您的迁移是否在那里?
如果是这样,您的任务就完成了,您可以继续前进。如果不是,且您的activities_newsitem表中包含该列,则需要伪造迁移以使您的南迁移历史记录保持最新状态。
虚假迁移:
python manage.py migrate activities --fake 0001
其中0001是迁移号码(所有迁移文件都以数字为前缀)。