无法添加或更新子行:外键约束失败(`dbName`.`#sql-e77_7`

Cannot add or update a child row: a foreign key constraint fails (`dbName`.`#sql-e77_7`

我正在尝试将一列添加到 table 并将其索引到另一列。执行迁移时,抛出错误:

Cannot add or update a child row: a foreign key constraint fails (`dbName`.`#sql-e77_7`, CONSTRAINT `fkName` FOREIGN KEY (`column`) REFERENCES `targetTable` (`targetColumn`) ON DELETE CASCADE ON UPDATE CASC)

任何人都可以向我解释一下“#sql-e77_7”是如何生成的吗?

如果找不到父项,则无法添加或更新行,您应该先插入包含父项的行,然后再插入子项。

举个例子 我有这两个 tables 练习和 exercice_entite

CREATE TABLE exercice
(
  exercice_id serial NOT NULL,
  date_debut date,
  date_fin date,
  exercice_etat text,
  exercice_code text,
  CONSTRAINT exercice_pkey PRIMARY KEY (exercice_id)
)

CREATE TABLE exercice_entite
(
  id_exercice_entite serial NOT NULL,
  id_exercice_pere integer,
  code_entite_filiale character varying(10),
  etat_exercice_entite text,
  code_exercice_entite text,
  code_entite_mm character varying(10),
  CONSTRAINT exercice_entite_pkey PRIMARY KEY (id_exercice_entite),
  CONSTRAINT exercice_entite_code_entite_filiale_fkey FOREIGN KEY (code_entite_filiale)
      REFERENCES entite_filiale (code_entite_filiale) MATCH SIMPLE
      ON UPDATE NO ACTION ON DELETE NO ACTION,
  CONSTRAINT exercice_entite_code_entite_mm_fkey FOREIGN KEY (code_entite_mm)
      REFERENCES entite_mm (code_entite) MATCH SIMPLE
      ON UPDATE NO ACTION ON DELETE NO ACTION,
  CONSTRAINT exercice_entite_id_exercice_pere_fkey FOREIGN KEY (id_exercice_pere)
      REFERENCES exercice (exercice_id) MATCH SIMPLE
      ON UPDATE NO ACTION ON DELETE NO ACTION
)

在这个例子中父 table 是 exercice ,子 table 是 exercice_entite 因为 table exercice_entite 中的每一行都有一个外键 id_exercice_pere 引用 table 练习 .

在 exercice_entite 中插入新行之前 mysql 检查练习中是否有一行 exercice_id 等于插入中 id_exercice_pere 的值行