| 15 | |
| 16 | The only way to workaround this now is to create a `migrations.RunPython(fk_remove)` statement before the `migrations.RemoveField`, to remove the foreign key manually, by getting its name from `INFORMATION_SCHEMA`: |
| 17 | |
| 18 | {{{ |
| 19 | def remove_fk(apps, schema_editor): |
| 20 | from django.db.backends.mysql.base import DatabaseWrapper |
| 21 | if isinstance(schema_editor.connection, DatabaseWrapper): |
| 22 | cursor = schema_editor.connection.cursor() |
| 23 | Edit = apps.get_model('base', 'Edit') |
| 24 | table_edit_name = Edit._meta.db_table |
| 25 | cursor.execute('SELECT DATABASE()') |
| 26 | db_name = cursor.fetchall()[0][0] |
| 27 | cursor.execute( |
| 28 | 'SELECT CONSTRAINT_NAME FROM INFORMATION_SCHEMA.KEY_COLUMN_USAGE WHERE' |
| 29 | ' `TABLE_SCHEMA`=\'%s\' AND `REFERENCED_TABLE_NAME`=\'%s\'' % ( |
| 30 | db_name, |
| 31 | table_edit_name |
| 32 | )) |
| 33 | result = cursor.fetchall() |
| 34 | if not result: |
| 35 | return |
| 36 | |
| 37 | Comment = apps.get_model('base', 'Comment') |
| 38 | table_comment_name = Comment._meta.db_table |
| 39 | fk_name = result[0][0] |
| 40 | query = 'ALTER TABLE `%s` DROP FOREIGN KEY `%s`' % ( |
| 41 | table_comment_name, |
| 42 | fk_name |
| 43 | ) |
| 44 | cursor.execute(query) |
| 45 | |
| 46 | }}} |