如果我运行此代码一切正常,但如果取消注释最后一个约束,我收到以下错误:
错误代码:1022。不能写;表格中的重复键'转移'
但没有其他密钥' fk_component_id',这段代码有什么问题?
-- -----------------------------------------------------
-- Table `pcdb`.`transfer`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `pcdb`.`transfer` (
`id` INT UNSIGNED NOT NULL AUTO_INCREMENT,
`component_id` INT UNSIGNED NOT NULL,
`type` INT NULL,
`parent_id` INT UNSIGNED NULL,
`source_id` INT UNSIGNED NULL,
`contractor_id` INT UNSIGNED NULL,
`src_department_id` INT UNSIGNED NULL,
`dest_department_id` INT UNSIGNED NULL,
`session_id` INT UNSIGNED NOT NULL,
`count` INT UNSIGNED NOT NULL,
`comment` TEXT(65535) NULL,
`active` TINYINT(1) NOT NULL DEFAULT 1,
PRIMARY KEY (`id`),
CONSTRAINT `fk_src_department_id`
FOREIGN KEY (`src_department_id`)
REFERENCES `pcdb`.`department` (`id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `fk_income_id`
FOREIGN KEY (`source_id`)
REFERENCES `pcdb`.`transfer` (`id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `fk_contractor_id`
FOREIGN KEY (`contractor_id`)
REFERENCES `pcdb`.`contractor` (`id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `fk_session_id`
FOREIGN KEY (`session_id`)
REFERENCES `pcdb`.`session` (`id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `fk_dest_department_id`
FOREIGN KEY (`dest_department_id`)
REFERENCES `pcdb`.`department` (`id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `fk_parent_id`
FOREIGN KEY (`parent_id`)
REFERENCES `pcdb`.`transfer` (`id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION/*,
CONSTRAINT `fk_component_id`
FOREIGN KEY (`component_id`)
REFERENCES `pcdb`.`component` (`id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION*/);
答案 0 :(得分:2)
听起来您可能已在数据库中的其他表上使用该名称进行约束。尝试将该约束的名称更改为“fk_transfer_component_id”,并查看是否仍然出现错误。