Search code examples
phpmysqlsymfonydoctrine-ormdoctrine-migrations

SQLSTATE[HY000]: General error: 1025 Error on rename error when dropping foreign key in Doctrine Migration


I am seeing the error below when I run the following query inside a Doctrine migration:

ALTER TABLE crmpicco_course_version DROP FOREIGN KEY FK_C060B146DE13F470

Migration 20151209153121 failed during Execution. 
Error An exception occurred while executing 
'ALTER TABLE crmpicco_course_version DROP FOREIGN KEY FK_C060B146DE13F470':

SQLSTATE[HY000]: General error: 
1025 Error on rename of './crmpicco_dev/crmpicco_course_version' 
to './crmpicco_dev/#sql2-77c-b0a' (errno: 152)

This is the table I am trying to change:

CREATE TABLE `crmpicco_course_version` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `course_id` int(11) NOT NULL,
  `updated_by_id` int(11) DEFAULT NULL,
  `name` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
  `start_date` datetime DEFAULT NULL,
  `end_date` datetime DEFAULT NULL,
  `created_at` datetime NOT NULL,
  `updated_at` datetime NOT NULL,
  PRIMARY KEY (`id`),
  KEY `IDX_C060B146896DBBDE` (`updated_by_id`),
  KEY `IDX_C060B146DE13F470` (`course_id`),
  CONSTRAINT `FK_C060B146896DBBDE` FOREIGN KEY (`updated_by_id`) REFERENCES `crmpicco_user` (`id`),
  CONSTRAINT `FK_C060B146DE13F470` FOREIGN KEY (`course_id`) REFERENCES `crmpicco_course` (`id`) ON DELETE CASCADE
) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci

What is preventing me from dropping this foreign key successfully?

When I run SHOW ENGINE INNODB STATUS I get the following:

------------------------
LATEST FOREIGN KEY ERROR
------------------------
151209 16:25:42 Error IN dropping of a FOREIGN KEY CONSTRAINT of TABLE "crmpicco_dev"."crmpicco_course_version",
IN SQL command
ALTER TABLE crmpicco_course_version DROP FOREIGN KEY FK_C060B146DE13F470
Cannot find a CONSTRAINT WITH the given id "FK_C060B146DE13F470".

Solution

  • After endless dropping and recreating of my local database I found that this was caused by Doctrine creating the same ALTER statement more than once and also in the wrong order.

    I had to change the statements around manually to make sure I migrated data from my old table to my new table before creating the new foreign key constraint on the new table. Without this change I was getting the error above and others.