自動生成された 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")
データベースが復元され、この移行が偶然に失敗しただけではないという確信を持って、この移行が 5 回繰り返されました。
私の質問は次のとおりです。何がうまくいかなかったのですか?