【问题标题】:Entire table missing after Django South migrationDjango South 迁移后整个表丢失
【发布时间】:2013-07-27 00:26:38
【问题描述】:

在自动生成的 Django South (0.7.6) 迁移文件中,它包含一个简单的前向迁移,该迁移删除了对 field 的唯一约束,然后将该字段作为另一个 Django 模型的外键。

class Migration(SchemaMigration):

    def forwards(self, orm):

        # Removing unique constraint on 'model2', fields ['field']
        db.delete_unique('app2_model2', ['field_id'])

        # Changing field 'model2.field'
        db.alter_column('app2_model2', 'field_id', self.gf('django.db.models.fields.related.ForeignKey')(null=True, to=orm['app1.model1']))

当我在 InnoDB 引擎上运行的 MySQL 5.5 数据库上执行该操作时,它“无法重命名”表

...
File "/opt/python/bundle/3/app/apps/app2/migrations/0020_auto__chg_field_model2_field__del_unique_model2_field__chg_field_model2.py", line 19, in forwards
    db.alter_column('app2_model2', 'field_id', self.gf('django.db.models.fields.related.ForeignKey')(null=True, to=orm['app1.model1']))
File "/opt/python/run/venv/lib/python2.6/site-packages/south/db/generic.py", line 44, in _cache_clear
    return func(self, table, *args, **opts)
File "/opt/python/run/venv/lib/python2.6/site-packages/south/db/generic.py", line 487, in alter_column
    self.delete_foreign_key(table_name, name)
File "/opt/python/run/venv/lib/python2.6/site-packages/south/db/generic.py", line 44, in _cache_clear
    return func(self, table, *args, **opts)
File "/opt/python/run/venv/lib/python2.6/site-packages/south/db/generic.py", line 780, in delete_foreign_key
    "constraint": self.quote_name(constraint_name),
File "/opt/python/run/venv/lib/python2.6/site-packages/south/db/generic.py", line 273, in execute
    cursor.execute(sql, params)
File "/opt/python/run/venv/lib/python2.6/site-packages/django/db/backends/mysql/base.py", line 114, in execute
    return self.cursor.execute(query, args)
File "/opt/python/run/venv/lib/python2.6/site-packages/MySQLdb/cursors.py", line 174, in execute
    self.errorhandler(self, exc, value)
File "/opt/python/run/venv/lib/python2.6/site-packages/MySQLdb/connections.py", line 36, in defaulterrorhandler
    raise errorclass, errorvalue
django.db.utils.DatabaseError: (1025, "Error on rename of './ebdb/#sql-260e_45b9' to './ebdb/app2_model2' (errno: 150)")

这意味着该表已被有效擦除,并且对 Django 不可见:

...
    cursor.execute(sql, params)
File "/opt/python/run/venv/lib/python2.6/site-packages/django/db/backends/mysql/base.py", line 114, in execute
    return self.cursor.execute(query, args)
File "/opt/python/run/venv/lib/python2.6/site-packages/MySQLdb/cursors.py", line 174, in execute
    self.errorhandler(self, exc, value)
File "/opt/python/run/venv/lib/python2.6/site-packages/MySQLdb/connections.py", line 36, in defaulterrorhandler
    raise errorclass, errorvalue
DatabaseError: (1146, "Table 'ebdb.app2_model2' doesn't exist")

数据库已恢复并重新运行此迁移,重复了五次,确信此迁移并非偶然失败。

我的问题是:出了什么问题?

【问题讨论】:

    标签: mysql innodb django-south database-migration


    【解决方案1】:

    发现问题。这发生在将 唯一一对一 Django 关系转换为简单 外键 关系的罕见组合中,使用 没有唯一性约束,在 MySQL 表上完成,MyISAM InnoDB,因为MySQL supports transactional schema alterations on neither,即使引擎这样做。

    为了解决这个问题,我刚刚删除了the advice of a kind fellow 之后的db.alter_column 命令,他发现OneToOne 关系和ForeignKey 关系之间的底层架构没有功能差异。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-08-14
      • 1970-01-01
      • 2012-08-06
      • 2012-08-13
      • 1970-01-01
      • 1970-01-01
      • 2014-03-10
      • 2011-09-28
      相关资源
      最近更新 更多