【问题标题】:Does Django's south (migration tool) work for innodb?Django 的南(迁移工具)是否适用于 innodb?
【发布时间】:2011-06-17 14:12:04
【问题描述】:
$ py manage.py  migrate turkey
Running migrations for turkey:
 - Migrating forwards to 0001_initial.
 > turkey:0001_initial
 ! 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.

! You *might* be able to recover with:   = DROP TABLE `turkey_demorecs` CASCADE; []

 ! The South developers regret this has happened, and would
 ! like to gently persuade you to consider a slightly
 ! easier-to-deal-with DBMS.
 ! NOTE: The error which caused the migration to fail is further up.

由于某种原因,我在尝试时得到了这个。 但我的其他设置在 MyISAM 中。

为什么它在 Innodb 中不起作用?

【问题讨论】:

    标签: python django django-south


    【解决方案1】:

    InnoDB 对外键有限制,可确保您在进行迁移时不会破坏数据库模型。 (见http://dev.mysql.com/doc/refman/5.5/en/innodb-foreign-key-constraints.html

    MyISAM 没有对约束的原生支持(尽管如果你选择做 http://dev.mysql.com/tech-resources/articles/mysql-enforcing-foreign-keys.html 似乎你可以实现它)

    因为 MyISAM 没有检查您的 FK 关系,所以您不会收到错误消息。然而,InnoDB 正在检查,您的迁移似乎有问题。

    【讨论】:

      【解决方案2】:

      另见https://code.djangoproject.com/wiki/AlterModelOnSyncDB

      在使用默认表存储引擎为 MyISAM 且我想使用 InnoDB 的 mysql 设置时,我遇到了同样的错误(使用上面链接中的配方,我们使用了post_syncdb 信号触发转换代码)。但是,当使用 South 创建新表时,它们首先使用 MyISAM 引擎创建,然后再进行转换。我错误地认为 InnoDB 表没有做它们应该做的事情,而实际上它们是 MyISAM;因为表是由信号转换的,所以任何迁移错误都将无法取消应用:-/

      如果您需要使用或创建默认为 MyISAM 的 InnoDB 表,可以通过以下方式解决:

      # add at the beginning of your migration
      if db.backend_name == 'mysql':
         db.execute('SET storage_engine=INNODB')
      

      或者如果您不介意性能下降:

      # add this to settings.py
      DATABASE_OPTIONS = {
         "init_command": "SET storage_engine=INNODB", # XXX: performance hit...
      }
      

      【讨论】:

      • 在使用配置为默认使用 MyISAM 的 mysql 时,South 给了我相同的“使用更易于处理的 DBMS”消息。我不能确定,但​​很可能是同样的问题。
      【解决方案3】:

      是的,South 确实支持 InnoDB。您可以删除“迁移”文件夹的内容,然后在此处重新运行 schemamigration、迁移并发布 0001_initial 文件的结果和内容吗? PS:请确保您已备份迁移文件夹或首先在源代码管理中。

      rm -fr app/migrations/*
      ./manage.py schemamigration app --initial
      ./manage.py migrate app
      

      【讨论】:

        【解决方案4】:

        您可以尝试添加到您的第一个迁移中:

        if db.backend_name == 'mysql':
            db.execute('SET foreign_key_checks=0')
        

        这将禁用外键检查约束。

        您不必将其设置回 1,因为它是一个会话变量。

        顺便说一句,如果您在迁移方法开始时设置为 0 并在迁移方法结束时设置回 1,则它不起作用,因为 south 与它们一起生成 SQL,但在它们返回时执行它。

        【讨论】:

          猜你喜欢
          • 2021-09-05
          • 1970-01-01
          • 2023-03-23
          • 2011-04-03
          • 1970-01-01
          • 2012-06-05
          • 1970-01-01
          • 2014-06-26
          相关资源
          最近更新 更多