MySQL Help Не удается удалить ограничение внешнего ключа строки (errno: 150) - PullRequest
2 голосов
/ 25 августа 2011

Uppon пытается удалить запись в одной из моих таблиц, я получил следующее.

#1451 - Cannot delete or update a parent row: a foreign key constraint fails (`pasremotedb`.`plans`, CONSTRAINT `FK_plans` FOREIGN KEY (`plan_id`) REFERENCES `plan_options` (`plan_id`))

Итак, я провел какое-то исследование, похоже, у меня испортились внешние ключи. Я пытался извлечь ключ, но потом получил это.

mysql> ALTER TABLE `plan_options` DROP INDEX `plan_id`;
ERROR 1025 (HY000): Error on rename of './pasremotedb/#sql-1c0f_31ea' to './pasremotedb/plan_options' (errno: 150)

провел больше исследований и решил сделать SHOW INNODB STATUS, чтобы получить LATEST FOREIGN KEY ERROR, который получился:

110824 15:07:33 Error in foreign key constraint of table pasremotedb/plans:
there is no index in referenced table which would contain
the columns as the first columns, or the data types in the
referenced table do not match the ones in table. Constraint:
,
  CONSTRAINT "FK_plans" FOREIGN KEY ("plan_id") REFERENCES "plan_options" ("plan_id")
The index in the foreign key in table is "PRIMARY"
See http://dev.mysql.com/doc/refman/5.1/en/innodb-foreign-key-constraints.html
for correct foreign key definition.

И это насколько я могу получить. Я пока не очень хорош в MySQL dbs и определенно зацикливаюсь на внешних ключах. Может кто-нибудь помочь?

Похоже, что задействованы две таблицы plans и plan_options. Обе структуры ниже:

--
-- Table structure for table `plan_options`
--

CREATE TABLE IF NOT EXISTS `plan_options` (
  `account_id` int(11) NOT NULL,
  `plan_id` tinyint(1) NOT NULL,
  `discipline_id` int(2) NOT NULL,
  `practice_type_id` int(1) NOT NULL,
  `discipline_other` varchar(100) NOT NULL,
  PRIMARY KEY (`account_id`),
  KEY `plan_id` (`plan_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

--
-- Dumping data for table `plan_options`
--

INSERT INTO `plan_options` (`account_id`, `plan_id`, `discipline_id`, `practice_type_id`, `discipline_other`) VALUES
(1, 3, 5, 1, ''),
(2, 2, 3, 1, ''),
(3, 1, 6, 1, ''),
(4, 2, 1, 2, ''),
(5, 3, 1, 1, ''),
(6, 2, 5, 1, ''),
(7, 2, 3, 1, ''),
(12, 2, 7, 2, 'MD'),
(13, 1, 2, 2, ''),
(14, 3, 1, 2, ''),
(16, 1, 1, 2, ''),
(18, 2, 7, 1, 'AMA Guides'),
(21, 2, 5, 1, '');

--
-- Constraints for dumped tables
--

--
-- Constraints for table `plan_options`
--
ALTER TABLE `plan_options`
  ADD CONSTRAINT `FK_plan_options` FOREIGN KEY (`account_id`) REFERENCES `account_details` (`account_id`);
--
-- Table structure for table `plans`
--

CREATE TABLE IF NOT EXISTS `plans` (
  `plan_id` tinyint(1) NOT NULL AUTO_INCREMENT,
  `plan_name` varchar(50) NOT NULL,
  PRIMARY KEY (`plan_id`)
) ENGINE=InnoDB  DEFAULT CHARSET=utf8 AUTO_INCREMENT=4 ;

--
-- Dumping data for table `plans`
--

INSERT INTO `plans` (`plan_id`, `plan_name`) VALUES
(1, 'Gold'),
(2, 'Platinum'),
(3, 'Supremacy');

--
-- Constraints for dumped tables
--

--
-- Constraints for table `plans`
--
ALTER TABLE `plans`
  ADD CONSTRAINT `FK_plans` FOREIGN KEY (`plan_id`) REFERENCES `plan_options` (`plan_id`);

1 Ответ

2 голосов
/ 25 августа 2011

Внешний ключ находится в таблице plan, , поэтому вы можете оставить его там :

ALTER TABLE `plans` DROP FOREIGN KEY `FK_plans`

Но имейте в виду, что этот ключ (и тот факт, что он останавливает вас) может быть желательным. Причина этого в том, чтобы предотвратить попадание опций в таблицу plan_options, которые не связаны с планом. Таким образом, вы можете удалить из обе таблицы при удалении:

DELETE FROM plans, plan_options
  WHERE plan_id = ?
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...