Changes between Version 1 and Version 2 of Ticket #24653


Ignore:
Timestamp:
Apr 16, 2015, 6:38:31 PM (10 years ago)
Author:
László Károlyi
Comment:

Legend:

Unmodified
Added
Removed
Modified
  • Ticket #24653 – Description

    v1 v2  
    1313
    1414This happens using python 3, might have to do something with #24390.
     15
     16The 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{{{
     19def 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}}}
Back to Top