0

If I run this code all work well, but if uncomment last constraint, I got the following error:

Error Code: 1022. Can't write; duplicate key in table 'transfer'

but there no another key 'fk_component_id', what wrong with this code?

-- -----------------------------------------------------
-- 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*/);
2
  • Is there a key in component_id? Commented May 3, 2015 at 6:02
  • -- ----------------------------------------------------- -- Table pcdb.component -- ----------------------------------------------------- CREATE TABLE IF NOT EXISTS pcdb.component ( id INT UNSIGNED NOT NULL AUTO_INCREMENT, name VARCHAR(255) NULL, PRIMARY KEY (id)); Commented May 3, 2015 at 6:17

1 Answer 1

2

It sounds like you may already have a constraint with that name on a different table in the database. Try changing the name of that constraint to something like "fk_transfer_component_id" and see if you still get the error.

Sign up to request clarification or add additional context in comments.

1 Comment

yes, it helps, I just think foreign key should be unique per table, but it seems that it should be unique in the entire scheme

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.