Page tree

Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

If in the first case the referenced table does exist, or in the second case the referenced data exists in the target table, then probably the foreign keys are damaged some how.

Solution for case 1:

Code Block
languagesql
titleExample SQL which had triggered the error in the past
CREATE TABLE `LEK_match_analysis_taskassoc` (
  `id` INT NOT NULL AUTO_INCREMENT,
  `taskGuid` VARCHAR(38) NULL,
  `created` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
  `internalFuzzy` TINYINT(1) NULL DEFAULT 0,
  `pretranslateMatchrate` INT(11) NULL,
  PRIMARY KEY (`id`),
  INDEX `fk_LEK_match_analysis_taskassoc_1_idx` (`taskGuid` ASC),
  CONSTRAINT `fk_LEK_match_analysis_taskassoc_1`
    FOREIGN KEY (`taskGuid`)
    REFERENCES `LEK_task` (`taskGuid`)
    ON DELETE CASCADE 
    ON UPDATE CASCADE);

...

What probably could also help: If there is a key on the foreign table affecting the field to be used as foreign key: recreate that index. (Not tested)

...

Solutions for case 2:

Solution A - inconsistent data types / collations / encodings

See https://stackoverflow.com/questions/16969060/mysql-error-1215-cannot-add-foreign-key-constraint

One example to get the error: 

  1. Livesystem DB System or DB itself is using encoding and/or collation A as default
  2. Testsystem DB System or DB itself is using encoding and/or collation B as default
  3. User makes a dump from live and applies it to Testsystem
  4. User then tries to update the Testsystem
    1. The dump from live creates table LEK_task with encoding/collation A
    2. The Update tries to create table XYZ with collation B
  5. => Result: the foreign key could not be created

Solution B - innoDB foreign keys corrupt

In that case the error happened on Updating data in the LEK_term_attributes table. Error was:

...