Modify

Ticket #66 (closed defect: fixed)

Opened 6 years ago

Last modified 6 years ago

delete_column() and ForeignKeys, with MySQL/Innodb

Reported by: michael@… Owned by: andrew
Priority: major Milestone:
Component: commands Version: 0.6-pre
Keywords: Cc:

Description

I'm using "startmigration --add-field Model.some_fk". Migration forwards works fine, but there seem to be two independent issues with backwards migration.

I have the following migration (as generated):

def forwards(self):
        # Mock model
        User = db.mock_model(model_name='User', db_table='auth_user', db_tablespace='', pk_field_name='id', pk_field_type=models.AutoField, pk_field_args=[], pk_field_kwargs={})

        # Adding field 'News.author'
        db.add_column('directory_news', 'author', models.ForeignKey(User))

    def backwards(self):        

        # Deleting field 'News.author'
        db.delete_column('directory_news', 'author')

Migrating backwards, I get:

OperationalError: (1091, "Can't DROP 'author'; check that column/key exists")

Apparently, South tries to drop "author" - but really would have to drop "author_id".

Even dropping "author_id" is not enough though, since this, using InnoDB, there is a ForeignKey? constraint, which needs to be dropped first. I.e. the correct SQL would be, for example:

ALTER TABLE directory_news DROP FOREIGN KEY author_id_refs_id_218608c7;
ALTER TABLE `directory_news` DROP COLUMN `author_id` CASCADE; []

Attachments

Change History

comment:1 Changed 6 years ago by andrew

  • Status changed from new to closed
  • Resolution set to fixed

Fixed in [123]; the delete_column call now correctly has _id on it, and the whole dropping-the-FKs-first thing doesn't seem to matter on my InnoDB; the CASCADE gets rid of them anyway.

If you still have an issue with the dropping of the column, open another ticket, and paste in the MySQL error (or whatever you can get, from show innodb status perhaps.)

View

Add a comment

Modify Ticket

Action
as closed
Author


E-mail address and user name can be saved in the Preferences.

 
Note: See TracTickets for help on using tickets.