简体   繁体   中英

MySQL Foreign Key Can't create table error:121

I've two tables

CREATE TABLE `patient_doctor_decision` (
  `id` bigint(50) NOT NULL,
  `help_entity_id` bigint(50) NOT NULL,
  `description` varchar(100) DEFAULT NULL,
  PRIMARY KEY (`id`)
);

CREATE TABLE `help_entity` (
  `id` bigint(50) NOT NULL AUTO_INCREMENT,
  `type` int(50) NOT NULL,
  `comments` varchar(100) DEFAULT NULL,
  PRIMARY KEY (`id`)
); 

Both tables already have a lot of data.

I want to establish a FK relationship on patient_doctor_decision.help_entity_id field with help_entity.id field.

ALTER TABLE `patient_doctor_decision`  
  ADD CONSTRAINT `fk_help_entity` FOREIGN KEY (`help_entity_id`) REFERENCES `help_entity`(`id`);

I've verified that foreign keys are valid and there is no foreign key which is not present in the referenced table as primary key.

SELECT * FROM patient_doctor_decision d WHERE d.help_entity_id NOT IN (
    SELECT id FROM help_entity);

0 rows are returned.

But when I execute the Alter statement mentioned above, I get this error

ERROR 1005: Can't create table 'mydb.#sql-57c_9' (errno: 121)

Please suggest.

好吧,我没有确切的工作方法,但是更改外键约束名称对我来说有效。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM