mysql - Odd 无法添加外键约束

标签 mysql foreign-keys ef-core-2.0 mysql-5.7

我有一个奇怪的。我无法使用以下内容创建表:

数据库中已经存在表Users,只需添加UserTimeZones,但会失败。

 CREATE TABLE `Users` (
  `AccessFailedCount` int(11) NOT NULL,
  `EmailConfirmed` bit(1) NOT NULL,
  `Id` char(36) NOT NULL,
  `NormalizedUserName` varchar(256) DEFAULT NULL,
  `NormalizedEmail` varchar(256) DEFAULT NULL,
  PRIMARY KEY (`Id`),
  UNIQUE KEY `UserNameIndex` (`NormalizedUserName`),
  KEY `EmailIndex` (`NormalizedEmail`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

CREATE TABLE `UserTimeZones` (
    `Id` char(36) NOT NULL,
    `UserId` char(36) NOT NULL,
    `TimeZoneOffsetInSeconds` int NOT NULL,
    `LastUpdatedAt` datetime(6) NOT NULL,
    CONSTRAINT `PK_UserTimeZones` PRIMARY KEY (`Id`),
    CONSTRAINT `FK_UserTimeZones_Users_UserId` FOREIGN KEY (`UserId`) REFERENCES `Users` (`Id`) ON DELETE CASCADE
);

SHOW ENGINE INNODB STATUS;

以下是状态显示的内容:

------------------------ LATEST FOREIGN KEY ERROR

2018-11-09 11:26:44 0x7f832c523700 Error in foreign key constraint of table fifty/UserTimeZones:

FOREIGN KEY (UserId) REFERENCES Users (Id) ON DELETE CASCADE ):

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/5.7/en/innodb-foreign-key-constraints.html for correct foreign key definition.

所以我有经典的“无法添加外键约束”。

我尝试过的:

  • 将 Users.Id 列放置为第一列:不会改变任何内容
  • 列类型相同,引擎也相同......
  • 在数据库中没有数据的情况下应用迁移 -> 它有效
  • 在没有数据的数据库中运行脚本 -> 仍然不起作用...

问题是什么?

不确定这是否重要,但我使用 Entity Framework 核心。

最佳答案

https://dev.mysql.com/doc/refman/8.0/en/innodb-foreign-key-constraints.html

Foreign key definitions for InnoDB tables are subject to the following conditions:

InnoDB permits a foreign key to reference any index column or group of columns. However, in the referenced table, there must be an index where the referenced columns are listed as the first columns in the same order.

但奇怪的是,引用表上似乎需要索引:

https://dev.mysql.com/doc/refman/8.0/en/create-table-foreign-keys.html#foreign-keys-examples

所以尝试在你的表上添加索引

CREATE TABLE `UserTimeZones` (
    `Id` char(36) NOT NULL,
    `UserId` char(36) NOT NULL,
    `TimeZoneOffsetInSeconds` int NOT NULL,
    `LastUpdatedAt` datetime(6) NOT NULL,
    CONSTRAINT `PK_UserTimeZones` PRIMARY KEY (`Id`),
    INDEX userid_ind (UserId),
    CONSTRAINT `FK_UserTimeZones_Users_UserId` FOREIGN KEY (`UserId`) REFERENCES `Users` (`Id`) ON DELETE CASCADE

);

注意:这是错误消息的内容。

关于mysql - Odd 无法添加外键约束,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53226539/

相关文章:

sql - MySQL - 如何在列内随机排列结果?

mysql,代码 "constraint"到底是做什么的?

c# - EF Core 更改跟踪 - 原始值和更改值的问题

c# - EF 核心自定义约定和代理

c# - EF core QueryFilter 的性能问题

php - SQL 计数查询返回预设为 1 的行数而不是实际行数

mysql - 使用 Laravel eloquent 或查询生成器匹配多个列

Mysql错误: Subquery returns more than 1 row

mysql - 更改字符集后无法添加外键约束

mysql - 将外键添加到现有表