MySQL Cluster - Composite foreign key error: Missing index for constraint
MySQL Cluster - Composite foreign key error: Missing index for constraint
我想将复合外键添加到我的 MySQL 集群 table。当我在本地尝试执行语句时,但在集群上我收到以下错误:
Failed to add the foreign key constraint. Missing index for constraint... in the referenced table 'y'
这是声明:
ALTER TABLE x
ADD CONSTRAINT fk_x_y
FOREIGN KEY (y_id, tenant_id)
REFERENCES y(id, tenant_id);
我已经为两个 table 执行了 SHOW FULL COLUMNS FROM
,每个 table 中的两列都是相同的。我在 id, tenant_id
y
table 中也有索引。
MySQL集群版本:8.0.25-cluster
编辑 1:
SHOW CREATE TABLE
结果:
Table x
:
CREATE TABLE `x` (
`id` bigint(20) NOT NULL AUTO_INCREMENT,
`tenant_id` bigint(20) DEFAULT NULL,
PRIMARY KEY (`id`),
KEY `fk_y_id_tenant_id` (`tenant_id`),
CONSTRAINT `fk_x_tenant_id` FOREIGN KEY (`tenant_id`) REFERENCES `tenant` (`id`) ON DELETE NO ACTION ON UPDATE NO ACTION
) ENGINE=ndbcluster AUTO_INCREMENT=446 DEFAULT CHARSET=utf8;
Table y
:
CREATE TABLE `reference_list` (
`id` bigint(20) NOT NULL AUTO_INCREMENT,
`tenant_id` bigint(20) NOT NULL,
PRIMARY KEY (`id`),
KEY `y_id_tenant_id` (`id`,`tenant_id`),
KEY `fk_y_id_tenant_id` (`tenant_id`),
CONSTRAINT `fk_y_id_tenant_id` FOREIGN KEY (`tenant_id`) REFERENCES `tenant` (`id`) ON DELETE NO ACTION ON UPDATE NO ACTION
) ENGINE=ndbcluster AUTO_INCREMENT=7 DEFAULT CHARSET=utf8;
我知道 tenant_id 在一个 table 中是 DEFAULT NULL
而在另一个中是 NOT_NULL
,但是将两者都更改为 DEFAULT NULL
并没有解决问题。
在 运行 之后:SHOW WARNINGS;
我发现我必须在 id
、tenant_id
列上添加 UNIQUE
索引。
我想将复合外键添加到我的 MySQL 集群 table。当我在本地尝试执行语句时,但在集群上我收到以下错误:
Failed to add the foreign key constraint. Missing index for constraint... in the referenced table 'y'
这是声明:
ALTER TABLE x
ADD CONSTRAINT fk_x_y
FOREIGN KEY (y_id, tenant_id)
REFERENCES y(id, tenant_id);
我已经为两个 table 执行了 SHOW FULL COLUMNS FROM
,每个 table 中的两列都是相同的。我在 id, tenant_id
y
table 中也有索引。
MySQL集群版本:8.0.25-cluster
编辑 1:
SHOW CREATE TABLE
结果:
Table x
:
CREATE TABLE `x` (
`id` bigint(20) NOT NULL AUTO_INCREMENT,
`tenant_id` bigint(20) DEFAULT NULL,
PRIMARY KEY (`id`),
KEY `fk_y_id_tenant_id` (`tenant_id`),
CONSTRAINT `fk_x_tenant_id` FOREIGN KEY (`tenant_id`) REFERENCES `tenant` (`id`) ON DELETE NO ACTION ON UPDATE NO ACTION
) ENGINE=ndbcluster AUTO_INCREMENT=446 DEFAULT CHARSET=utf8;
Table y
:
CREATE TABLE `reference_list` (
`id` bigint(20) NOT NULL AUTO_INCREMENT,
`tenant_id` bigint(20) NOT NULL,
PRIMARY KEY (`id`),
KEY `y_id_tenant_id` (`id`,`tenant_id`),
KEY `fk_y_id_tenant_id` (`tenant_id`),
CONSTRAINT `fk_y_id_tenant_id` FOREIGN KEY (`tenant_id`) REFERENCES `tenant` (`id`) ON DELETE NO ACTION ON UPDATE NO ACTION
) ENGINE=ndbcluster AUTO_INCREMENT=7 DEFAULT CHARSET=utf8;
我知道 tenant_id 在一个 table 中是 DEFAULT NULL
而在另一个中是 NOT_NULL
,但是将两者都更改为 DEFAULT NULL
并没有解决问题。
在 运行 之后:SHOW WARNINGS;
我发现我必须在 id
、tenant_id
列上添加 UNIQUE
索引。