【问题标题】:South ManyToMany migration南多对多迁移
【发布时间】:2012-07-06 16:22:49
【问题描述】:

我有一个现有的 Django 模型,它引用代理模型 users.User 作为 ManyToMany 字段。我最近为模型创建了一个初始的南迁移,并使用 --fake 标志毫无问题地应用它。

现在,如果我重新克隆我的存储库,运行 syncdb,然后尝试应用我的迁移,我会在处理具有 ManyToMany 字段的模型时收到此 South 错误:

Running migrations for <app>:
 - Migrating forwards to 0001_initial.
 > <app>:0001_initial
Traceback (most recent call last):
  File "manage.py", line 14, in <module>
    execute_manager(settings)
... (traceback)
raise KeyError("The model '%s' from the app '%s' is not available in this migration." %    (model, app))
KeyError: "The model 'user' from the app 'users' is not available in this migration."

我是否需要每次都为这个特定应用伪造迁移?有人遇到过这个错误吗?

【问题讨论】:

    标签: django django-south


    【解决方案1】:

    为什么 users.User 是代理模型?我认为,在克隆的存储库中,您必须只使用 south 而没有 syncdb。

    【讨论】:

      【解决方案2】:

      这是因为 South(在当前版本 0.8.4 中)无法为具有代理模型的多对多关系创建正确的迁移。 There is a open ticket for this issue。尽管您可以使用与直通类的多对多关系来解决它。

      【讨论】:

        【解决方案3】:

        因为它仍然是一个未解决的问题(在 South 存储库中,就像在我们的程序员生活中一样 :)),这是我通常的解决方法:

        1。新安装

        django-admin.py syncdb --all
        django-admin.py migrate --fake
        

        2。修复迁移(手动)

        (之前)

            db.create_table(m2m_table_name, (
                ('id', models.AutoField(verbose_name='ID', primary_key=True, auto_created=True)),
                ('message', models.ForeignKey(orm[u'email.message'], null=False)),
                ('avcpuser', models.ForeignKey(orm[u'profiles.avcpuser'], null=False)) # this is the troublesome proxy model (profiles.avcpuser is my proxy towards auth.User
            ))
        

        (之后)

            db.create_table(m2m_table_name, (
                ('id', models.AutoField(verbose_name='ID', primary_key=True, auto_created=True)),
                ('message', models.ForeignKey(orm[u'email.message'], null=False)),
                ('avcpuser', models.ForeignKey(orm[u'auth.User'], null=False)) # replace it with a ForeignKey to the original model
            ))
        

        老实说,我不是 100% 确定这个修复是“合法的”并且可以被列为“最佳实践”,但到目前为止,它并没有给我维护现有和新项目带来任何问题。为了我的辩护,我想到这个想法时注意到,当 South 为代理模型生成一对多的 ForeignKey 时,它会针对原始模型执行此操作。

        【讨论】:

          猜你喜欢
          • 2011-08-30
          • 2013-10-24
          • 1970-01-01
          • 1970-01-01
          • 2014-09-17
          • 2023-04-07
          • 2021-12-27
          • 1970-01-01
          • 2011-04-03
          相关资源
          最近更新 更多