【问题标题】:South scheme migration apparently didn't work南方计划迁移显然没有奏效
【发布时间】:2014-06-05 20:03:30
【问题描述】:

好的,所以我有一个名为 locationmanager 的应用程序,其模型如下所示:

class Location(models.Model):
    region = models.ForeignKey(Region)

我想把它改成这个

class Location(models.Model):
    region = models.ForeignKey(Region, blank=True, null=True)

更改后,我运行了如下所示的架构迁移:

    $ python manage.py schemamigration locationmanager --auto
     ? The field 'Location.region' does not have a default specified, yet is NOT NULL.
     ? Since you are making this field nullable, you MUST specify a default
     ? value to use for existing rows. Would you like to:
     ?  1. Quit now.
     ?  2. Specify a one-off value to use for existing columns now
     ?  3. Disable the backwards migration by raising an exception; you can edit the migration to fix it later
     ? Please select a choice: 2
     ? Please enter Python code for your one-off default value.
     ? The datetime module is available, so you can do e.g. datetime.date.today()
     >>> 0
     ~ Changed field region on locationmanager.Location
     ? The field 'Location.manager' does not have a default specified, yet is NOT NULL.
     ? Since you are making this field nullable, you MUST specify a default
     ? value to use for existing rows. Would you like to:
     ?  1. Quit now.
     ?  2. Specify a one-off value to use for existing columns now
     ?  3. Disable the backwards migration by raising an exception; you can edit the migration to fix it later
     ? Please select a choice: 0
     ! Invalid choice.
     ? Please select a choice: 2
     ? Please enter Python code for your one-off default value.
     ? The datetime module is available, so you can do e.g. datetime.date.today()
     >>> 0
     ~ Changed field manager on locationmanager.Location
    Created 0003_auto__chg_field_location_region__chg_field_location_manager.py. You can now apply this migration with: ./manage.py migrate locationmanager

$ sudo python manage.py migrate locationmanager --fake
Running migrations for locationmanager:
 - Migrating forwards to 0003_auto__chg_field_location_region__chg_field_location_manager.
 > locationmanager:0002_initial
   (faked)
 > locationmanager:0003_auto__chg_field_location_region__chg_field_location_manager
   (faked)

运行测试后,表单向我吐出错误:

   IntegrityError at /dash/location/add
    locationmanager_location.region_id may not be NULL

我以为我改变了它?我做错了什么?我目前可以做些什么来解决这个问题?

【问题讨论】:

    标签: python django django-models django-south


    【解决方案1】:

    您指定 --fake 迁移。伪造您的初始迁移以达到当前状态(更改之前),并且它们会定期迁移。

    编辑(基于 cmets):

    为了修复迁移,您必须使用回滚到迁移 0002

    ./manage.py migrate locationmanager 0002
    

    迁移 0002 是伪造的,这很好,因为您可能不希望南更改更改前状态的 db 方案。

    现在您可以使用

    定期迁移到迁移 0003
    ./manage.py migrate locationmanager
    

    请注意,您始终可以使用

    查看已应用迁移的列表
    ./manage.py migrate locationmanager --list
    

    【讨论】:

    • 什么?如终端所示,我在迁移中使用了 --fake 。您如何/为什么要 --fake 初始迁移?
    • @ApathyBear 你为什么要伪造迁移?伪造它只会将迁移标记为已完成,但不会在数据库级别执行任何操作
    • 嗯,很高兴知道。我应该怎么做而不是@CésarBustíos
    • @ApathyBear 我猜你可以回到以前的迁移并在没有 --fake 标志的情况下再次迁移
    猜你喜欢
    • 2016-07-27
    • 1970-01-01
    • 1970-01-01
    • 2014-10-28
    • 2012-04-17
    • 2011-04-03
    • 2019-02-14
    • 1970-01-01
    相关资源
    最近更新 更多