我试图将我的新模式转发到我的数据库服务器上,但我不知道为什么我会得到这个错误。

我试图在这里寻找答案,但我所找到的一切都表明,要么将数据库引擎设置为InnoDB,要么确保我试图用作外键的键是它们自己表中的主键。如果我没记错的话,这两件事我都做过。我还能做什么?

Executing SQL script in server

ERROR: Error 1215: Cannot add foreign key constraint

-- -----------------------------------------------------
-- Table `Alternative_Pathways`.`Clients_has_Staff`
-- -----------------------------------------------------
CREATE  TABLE IF NOT EXISTS `Alternative_Pathways`.`Clients_has_Staff` (
  `Clients_Case_Number` INT NOT NULL ,
  `Staff_Emp_ID` INT NOT NULL ,
  PRIMARY KEY (`Clients_Case_Number`, `Staff_Emp_ID`) ,
  INDEX `fk_Clients_has_Staff_Staff1_idx` (`Staff_Emp_ID` ASC) ,
  INDEX `fk_Clients_has_Staff_Clients_idx` (`Clients_Case_Number` ASC) ,
  CONSTRAINT `fk_Clients_has_Staff_Clients`
    FOREIGN KEY (`Clients_Case_Number` )
    REFERENCES `Alternative_Pathways`.`Clients` (`Case_Number` )
    ON DELETE NO ACTION
    ON UPDATE NO ACTION,
  CONSTRAINT `fk_Clients_has_Staff_Staff1`
    FOREIGN KEY (`Staff_Emp_ID` )
    REFERENCES `Alternative_Pathways`.`Staff` (`Emp_ID` )
    ON DELETE NO ACTION
    ON UPDATE NO ACTION)
ENGINE = InnoDB

SQL脚本执行完成:语句:7成功,1失败

下面是父表的SQL。

CREATE  TABLE IF NOT EXISTS `Alternative_Pathways`.`Clients` (
  `Case_Number` INT NOT NULL ,
  `First_Name` CHAR(10) NULL ,
  `Middle_Name` CHAR(10) NULL ,
  `Last_Name` CHAR(10) NULL ,
  `Address` CHAR(50) NULL ,
  `Phone_Number` INT(10) NULL ,
  PRIMARY KEY (`Case_Number`) )
ENGINE = InnoDB

CREATE  TABLE IF NOT EXISTS `Alternative_Pathways`.`Staff` (
  `Emp_ID` INT NOT NULL ,
  `First_Name` CHAR(10) NULL ,
  `Middle_Name` CHAR(10) NULL ,
  `Last_Name` CHAR(10) NULL ,
  PRIMARY KEY (`Emp_ID`) )
ENGINE = InnoDB

当前回答

在我的情况下,我不得不禁用外键检查,因为源表不存在。

设置FOREIGN_KEY_CHECKS = 0;

其他回答

也要注意反节拍音的使用。我在一个脚本中有如下语句

ALTER TABLE service ADD FOREIGN KEY (create_by) REFERENCES `system_user(id)`;

但最后的反节拍是假的。它应该是:

ALTER TABLE service ADD FOREIGN KEY (create_by) REFERENCES `system_user`(`id`);

不幸的是MySQL没有给出这个错误的任何细节…

我找不到这个错误

CREATE TABLE RATING (

Riv_Id INT(5),
Mov_Id INT(10) DEFAULT 0,
Stars INT(5),
Rating_date DATE, 

PRIMARY KEY (Riv_Id, Mov_Id),

FOREIGN KEY (Riv_Id) REFERENCES REVIEWER(Reviewer_ID)
ON DELETE SET NULL ON UPDATE CASCADE,

FOREIGN KEY (Mov_Id) REFERENCES MOVIE(Movie_ID)
ON DELETE SET DEFAULT ON UPDATE CASCADE
)

MySQL (InnoDB)…获取想要链接的列的定义:

SELECT * FROM information_schema.columns WHERE
TABLE_NAME IN (tb_name','referenced_table_name') AND
COLUMN_NAME  IN ('col_name','referenced_col_name')\G

比较并验证两个列定义具有:

相同的COLUMN_TYPE(长度),相同的COLATION

可能需要禁用/启用foreign_key机制,但要注意在生产环境中:

set foreign_key_checks=0;
ALTER TABLE tb_name ADD FOREIGN KEY(col_name) REFERENCES ref_table(ref_column) ON DELETE ...
set foreign_key_checks=1;

你可能会得到外键约束错误的原因:

You are not using InnoDB as the engine on all tables. You are trying to reference a nonexistent key on the target table. Make sure it is a key on the other table (it can be a primary or unique key, or just a key) The types of the columns are not the same (an exception is the column on the referencing table can be nullable even if it is not nullable in the referenced table). If the primary key or foreign key is a varchar, make sure the collation is the same for both. One of the reasons may also be that the column you are using for ON DELETE SET NULL is not defined to be null. So make sure that the column is set default null.

检查这些。

我只是想为VARCHAR外键关系添加这种情况。我花了上周的时间在MySQL Workbench 8.0中试图解决这个问题,最终能够修复这个错误。

简短的回答: 模式、表、列、引用表、引用列和引用父表的任何其他表的字符集和排序规则必须匹配。

长一点的回答: 我的表中有一个ENUM数据类型。我将此更改为VARCHAR,我可以从引用表中获取值,这样我就不必更改父表来添加额外的选项。这个外键关系看起来很简单,但我得到了1215错误。Arvind的回答和下面的链接建议使用

SHOW ENGINE INNODB STATUS;

在使用这个命令时,我得到了以下详细的错误描述,没有其他有用的信息

Cannot find an index in the referenced table where the referenced columns appear as the first columns, or column types in the table and the referenced table do not match for constraint. Note that the internal storage type of ENUM and SET changed in tables created with >= InnoDB-4.1.12, and such columns in old tables cannot be referenced by such columns in new tables. Please refer to http://dev.mysql.com/doc/refman/8.0/en/innodb-foreign-key-constraints.html for correct foreign key definition.

之后,我使用SET FOREIGN_KEY_CHECKS=0;正如Arvind Bharadwaj所建议的,链接在这里:

这给出了以下错误信息:

错误码:1822。日志含义添加外键约束失败。失踪 约束索引

在这一点上,我对模式进行了“逆向工程”,并且能够在EER图中创建外键关系。在'forward engineer'-ing上,我得到了以下错误:

错误1452:不能添加或更新子行:外键约束 失败

当我将EER图“转发工程”到一个新模式时,SQL脚本运行时没有出现问题。通过比较转发工程师尝试生成的SQL,我发现不同之处在于字符集和排序规则。父表、子表和两列具有utf8mb4字符集和utf8mb4_0900_ai_ci排序规则,然而,父表中的另一列使用character set = utf8, COLLATE = utf8_bin引用;到不同的子表。

对于整个模式,我更改了所有表和所有列的字符集和排序规则如下:

CHARACTER SET = utf8mb4 COLLATE = utf8mb4_general_ci;

这最终解决了我1215错误的问题。

注: 排序规则utf8mb4_general_ci适用于MySQL Workbench 5.0或更高版本。排序utf8mb4_0900_ai_ci仅适用于MySQL Workbench 8.0或更高版本。我相信我遇到字符集和排序问题的原因之一是由于MySQL Workbench升级到8.0之间。这里有一个链接,详细介绍了这种排序方式。