Skip to content

Instantly share code, notes, and snippets.

@weaverryan
Created September 16, 2016 14:57
KnpUniversity Migrations fix for MySQL 5.6.7 and later

In MySQL 5.6.7, MySQL changed the behavior of foreign key constraints. You can read about it here: http://stackoverflow.com/questions/17015844/mysql-5-6-foreign-key-constraint-error-didnt-occur-in-5-5

This means that you may need to update your migration just a little bit in order for it to work: Doctrine hasn't (yet) updated their code to be smart enough to do this for us in new MySQL versions. The attached migration file should replace the one generated in this chapter: https://knpuniversity.com/screencast/doctrine-relations/join-column-relation-fixtures. It explicitly drops the foreign key column, changes the column, then re-adds it.

Do that, and everything should be fine :).

Cheers!

<?php
// inside the class
// ...
public function up(Schema $schema)
{
// this up() migration is autogenerated, please modify it to your needs
$this->abortIf($this->connection->getDatabasePlatform()->getName() != "mysql");
// drop the old FKey
$this->addSql("ALTER TABLE genus_note DROP FOREIGN KEY FK_6478FCEC85C4074C");
$this->addSql("DROP INDEX IDX_6478FCEC85C4074C ON genus_note");
// alter
$this->addSql("ALTER TABLE genus_note CHANGE genus_id genus_id INT NOT NULL");
// re-add the FKEY
$this->addSql("ALTER TABLE genus_note ADD CONSTRAINT FK_6478FCEC85C4074C FOREIGN KEY (genus_id) REFERENCES genus (id)");
$this->addSql("CREATE INDEX IDX_6478FCEC85C4074C ON genus_note (genus_id)");
}
}
@yahyaerturan
Copy link

Thank you.

@WinterUni
Copy link

Thank You

@derflallys
Copy link

Thanks !

@technet-systems
Copy link

Worked just fine :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment