【问题标题】:South Data Migration With OneToOne Key使用OneToOne Key进行南方数据迁移
【发布时间】:2013-03-18 02:17:09
【问题描述】:

我正在使用 South 进行 sqlite3 数据迁移。我的旧架构具有以下 UserProfile 模型:

class UserProfile(models.Model):
    user = models.OneToOneField(User)
    weekOne = models.OneToOneField(WeekOne)
    weekTwo = models.OneToOneField(WeekTwo)
    weekThree = models.OneToOneField(WeekThree)

但我感觉添加了一些新的周,即 weekFour、weekFive、weekSix 等。每周本身就是一个模型,继承自通用的 Week 模型。所以 Week 模型的基本原型是这样的:

class WeekOne(Week):
    name = u'Week One'
    exercises = ['squats', 'lunges', 'stairDaysCount', 'skipStairs']
    # Required benchmarks for given exercises
    squatBenchmark = 1000
    lungeBenchmark = 250
    stairDaysCountBenchmark = 3

    totalGoals = 4

我的问题是,我应该在我的数据迁移代码中添加什么样的代码,以便我可以在额外的几周内填充旧的 UserProfiles。我有这样的想法:

def forwards(self, orm):
    for user in orm.UserProfile.objects.all():
        user.weekFour = orm.WeekFour()
        user.weekFive = orm.weekFive()
        # etc.

但这似乎不起作用。当我尝试运行架构迁移时出现此错误:

Migration 'my_app:0002_newWeeks' is marked for no-dry-run

然后是这个:

DatabaseError: no such column: my_app_userprofile.weekFour_id

【问题讨论】:

    标签: python database django django-south


    【解决方案1】:

    只需在终端中运行它

     python manage.py schemamigration myapp --auto
    

    然后

     python manage.py migrate
    

    【讨论】:

    • 如果我的应用已经创建了没有新密钥的模型,这将不起作用。即,我创建了一个以第 1-3 周为键的用户,用这些键和该用户做了很多事情,然后添加了第 4-9 周。我所有的新用户都将拥有所有周数,但老用户将在第 4-9 周内为 null,并且键是 NOT NULL 键,因此这会导致错误。
    猜你喜欢
    • 1970-01-01
    • 2014-05-29
    • 1970-01-01
    • 2011-12-07
    • 2015-10-30
    • 2014-07-30
    • 2012-01-11
    • 2012-11-21
    • 2018-12-29
    相关资源
    最近更新 更多