2

当我在 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".
4

1 回答 1

2

在无休止地删除和重新创建我的本地数据库之后,我发现这是由于 DoctrineALTER多次创建相同的语句并且顺序错误造成的。

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

于 2015-12-15T09:58:21.003 回答