I use MySQL and doctrine/migrations.
After running doctrine:migrations:diff for a particular database schema, I obtain the following in my migration file:
public function up(Schema $schema)
...
$this->addSql('ALTER TABLE user_group DROP FOREIGN KEY FK_983D1F3EFE54D947');
$this->addSql('ALTER TABLE user_group DROP FOREIGN KEY FK_983D1F3EA76ED395');
$this->addSql('DROP INDEX idx_983d1f3ea76ed395 ON user_group');
$this->addSql('CREATE INDEX IDX_81305A8A76ED395 ON user_group (user_id)');
$this->addSql('DROP INDEX idx_983d1f3efe54d947 ON user_group');
$this->addSql('CREATE INDEX IDX_81305A8FE54D947 ON user_group (group_id)');
$this->addSql('ALTER TABLE user_group ADD CONSTRAINT FK_983D1F3EFE54D947 FOREIGN KEY (group_id) REFERENCES group (id)');
$this->addSql('ALTER TABLE user_group ADD CONSTRAINT FK_983D1F3EA76ED395 FOREIGN KEY (user_id) REFERENCES user (id)');
...
public function down(Schema $schema)
...
$this->addSql('ALTER TABLE user_group DROP FOREIGN KEY FK_81305A8A76ED395');
$this->addSql('ALTER TABLE user_group DROP FOREIGN KEY FK_81305A8FE54D947');
$this->addSql('DROP INDEX idx_81305a8a76ed395 ON user_group');
$this->addSql('CREATE INDEX IDX_983D1F3EA76ED395 ON user_group (user_id)');
$this->addSql('DROP INDEX idx_81305a8fe54d947 ON user_group');
$this->addSql('CREATE INDEX IDX_983D1F3EFE54D947 ON user_group (group_id)');
$this->addSql('ALTER TABLE user_group ADD CONSTRAINT FK_81305A8A76ED395 FOREIGN KEY (user_id) REFERENCES user (id)');
$this->addSql('ALTER TABLE user_group ADD CONSTRAINT FK_81305A8FE54D947 FOREIGN KEY (group_id) REFERENCES group (id)');
Running doctrine:migrations:execute migration_nr is ok. However doctrine:migrations:execute migration_nr --down throws the following error:
Migration migration_nr failed during Execution. Error An exception occurred while executing 'ALTER TABLE user_group DROP FOREIGN KEY FK_81305A8A76ED395':
SQLSTATE[HY000]: General error: 1025 Error on rename of './my_project/user_group' to './my_project/#sql2-1d9-bc' (errno: 152)
When I change manually the foreign keys in the down() method, by replacing in the first two lines FK_81305A8A76ED395 into FK_983D1F3EFE54D947, it works.
I suppose the generated foreign keys in the down() method are invalid.
I use MySQL and doctrine/migrations. After running doctrine:migrations:diff for a particular database schema, I obtain the following in my migration file:
public function up(Schema $schema) ...
$this->addSql('ALTER TABLE user_group DROP FOREIGN KEY FK_983D1F3EFE54D947'); $this->addSql('ALTER TABLE user_group DROP FOREIGN KEY FK_983D1F3EA76ED395'); $this->addSql('DROP INDEX idx_983d1f3ea76ed395 ON user_group'); $this->addSql('CREATE INDEX IDX_81305A8A76ED395 ON user_group (user_id)'); $this->addSql('DROP INDEX idx_983d1f3efe54d947 ON user_group'); $this->addSql('CREATE INDEX IDX_81305A8FE54D947 ON user_group (group_id)'); $this->addSql('ALTER TABLE user_group ADD CONSTRAINT FK_983D1F3EFE54D947 FOREIGN KEY (group_id) REFERENCES group (id)'); $this->addSql('ALTER TABLE user_group ADD CONSTRAINT FK_983D1F3EA76ED395 FOREIGN KEY (user_id) REFERENCES user (id)');
... public function down(Schema $schema) ... $this->addSql('ALTER TABLE user_group DROP FOREIGN KEY FK_81305A8A76ED395'); $this->addSql('ALTER TABLE user_group DROP FOREIGN KEY FK_81305A8FE54D947'); $this->addSql('DROP INDEX idx_81305a8a76ed395 ON user_group'); $this->addSql('CREATE INDEX IDX_983D1F3EA76ED395 ON user_group (user_id)'); $this->addSql('DROP INDEX idx_81305a8fe54d947 ON user_group'); $this->addSql('CREATE INDEX IDX_983D1F3EFE54D947 ON user_group (group_id)'); $this->addSql('ALTER TABLE user_group ADD CONSTRAINT FK_81305A8A76ED395 FOREIGN KEY (user_id) REFERENCES user (id)'); $this->addSql('ALTER TABLE user_group ADD CONSTRAINT FK_81305A8FE54D947 FOREIGN KEY (group_id) REFERENCES group (id)');
Running
doctrine:migrations:execute migration_nr
is ok. Howeverdoctrine:migrations:execute migration_nr --down
throws the following error:Migration migration_nr failed during Execution. Error An exception occurred while executing 'ALTER TABLE user_group DROP FOREIGN KEY FK_81305A8A76ED395':
SQLSTATE[HY000]: General error: 1025 Error on rename of './my_project/user_group' to './my_project/#sql2-1d9-bc' (errno: 152)
When I change manually the foreign keys in the down() method, by replacing in the first two lines FK_81305A8A76ED395 into FK_983D1F3EFE54D947, it works.
I suppose the generated foreign keys in the down() method are invalid.