Djangoの移行で例外をキャッチするにはどうすればよいですか?Djangoの移行で例外を処理する方法は?
さまざまな遺産の理由から、私は時々失敗することを期待しています。私は、そのエラーをキャッチし、その場合にはエラー処理コードを実行できるようにしたい。
具体的には、テーブルの名前を変更していますが、宛先テーブルがすでに存在していて、古いテーブルと新しいテーブルの内容をマージして古いものを削除したい場合があります。 。私は(:()はDjango 1.7を実行していると我々は1.8へのアップグレードを計画しているが、それはまだ起きていない
私の移行は次のとおりです。
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
from django.db import models, migrations
class Migration(migrations.Migration):
dependencies = [
('main', '0007_migration_name'),
]
operations = [
migrations.AlterModelTable(
name='table_name',
table='LegacyTableName',
),
]
私はこれを実行します、私はすべてのことが、移行自体に提供されます
Traceback (most recent call last):
File "manage.py", line 10, in <module>
execute_from_command_line(sys.argv)
File ".../django/core/management/__init__.py", line 385, in execute_from_command_line
utility.execute()
File ".../django/core/management/__init__.py", line 377, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File ".../django/core/management/base.py", line 288, in run_from_argv
self.execute(*args, **options.__dict__)
File ".../django/core/management/base.py", line 338, in execute
output = self.handle(*args, **options)
File ".../django/core/management/commands/migrate.py", line 161, in handle
executor.migrate(targets, plan, fake=options.get("fake", False))
File ".../django/db/migrations/executor.py", line 68, in migrate
self.apply_migration(migration, fake=fake)
File ".../django/db/migrations/executor.py", line 102, in apply_migration
migration.apply(project_state, schema_editor)
File ".../django/db/migrations/migration.py", line 108, in apply
operation.database_forwards(self.app_label, schema_editor, project_state, new_state)
File ".../django/db/migrations/operations/models.py", line 236, in database_forwards
new_model._meta.db_table,
File ".../django/db/backends/schema.py", line 350, in alter_db_table
"new_table": self.quote_name(new_db_table),
File ".../django/db/backends/schema.py", line 111, in execute
cursor.execute(sql, params)
File ".../django/db/backends/utils.py", line 81, in execute
return super(CursorDebugWrapper, self).execute(sql, params)
File ".../django/db/backends/utils.py", line 65, in execute
return self.cursor.execute(sql, params)
File ".../django/db/utils.py", line 94, in __exit__
six.reraise(dj_exc_type, dj_exc_value, traceback)
File ".../django/db/backends/utils.py", line 65, in execute
return self.cursor.execute(sql, params)
File ".../django/db/backends/mysql/base.py", line 129, in execute
return self.cursor.execute(query, args)
File ".../MySQLdb/cursors.py", line 226, in execute
self.errorhandler(self, exc, value)
File ".../MySQLdb/connections.py", line 36, in defaulterrorhandler
raise errorvalue
django.db.utils.OperationalError: (1050, "Table 'LegacyTableName' already exists")
がoperations
リストで取得し、the docsでオプションのエラー処理パラメータがあるようには思えない。
OperationalErrorをキャッチすると、テーブルをマージするためにPythonを実行できますか?
あなたはバックエンドとは何も言及していません。この質問に対する答えは、それに非常に依存しています。 – e4c5
Perconaですが、私はこれをより一般的な方法でPythonで処理したいと考えていました。 – Hwesta
これは非常に難しいでしょう。 – e4c5