SQLSTATE [HY000]:常规错误:1025在Doctrine Migration中删除外键时重命名错误时出错

时间:2015-12-09 15:50:01

标签: php mysql symfony doctrine-orm doctrine-migrations

当我在Doctrine迁移中运行以下查询时,我看到了以下错误:

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)

这是我要改变的表格:

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

是什么阻止我成功删除这个外键?

当我运行SHOW ENGINE INNODB STATUS时,我得到以下内容:

------------------------
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".

1 个答案:

答案 0 :(得分:1)

在无休止地删除并重新创建我的本地数据库之后,我发现这是由Doctrine以错误的顺序多次创建相同的ALTER语句以及引起的。

我必须手动更改语句,以确保在新表上创建新的外键约束之前将数据从旧表迁移到新表。如果没有这个改变,我会得到上面的错误和其他错误。