"Foreign key constraint is incorrectly formed" 创建唯一索引时

"Foreign key constraint is incorrectly formed" while creating unique index

我试图执行以下查询:

CREATE TABLE `lob_sections` (
 `id` int(11) NOT NULL AUTO_INCREMENT,
 `section_name` varchar(600) NOT NULL,
 `lob_type` varchar(64) NOT NULL,
 `agency_id` varchar(64) NOT NULL,
 `display_order` tinyint(2) NOT NULL DEFAULT 1,
 `active` tinyint(1) NOT NULL DEFAULT 1,
 `created_date` timestamp NOT NULL DEFAULT current_timestamp(),
 `last_modified_date` timestamp NOT NULL DEFAULT current_timestamp() ON UPDATE current_timestamp(),
 PRIMARY KEY (`id`),
 UNIQUE KEY `unq_lob_sections` (`agency_id`,`lob_type`,`section_name`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;


CREATE TABLE `lob_custom_fields` (
 `id` int(11) NOT NULL AUTO_INCREMENT,
 `section_id` int(11) NOT NULL,
 `field_label` varchar(1400) NOT NULL,
 `field_type` varchar(20) NOT NULL,
 `active` tinyint(1) NOT NULL DEFAULT 1,
 `display_order` tinyint(3) NOT NULL DEFAULT 1,
 `required` tinyint(1) NOT NULL DEFAULT 0,
 `created_date` timestamp NOT NULL DEFAULT current_timestamp(),
 `last_modified_date` timestamp NOT NULL DEFAULT current_timestamp() ON UPDATE current_timestamp(),
 PRIMARY KEY (`id`),
 CONSTRAINT unq_section_field_label UNIQUE (section_id, field_label),
 CONSTRAINT `fk_section_id` FOREIGN KEY (`section_id`) REFERENCES `lob_sections` (`id`) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

lob_sections table 创建成功,但 lob_custom_fields 没有创建,它抛出以下错误:

#1005 - Can't create table abc_db.lob_custom_fields (errno: 150 "Foreign key constraint is incorrectly formed") (Details…)

当我点击详细信息时,它会显示原因“Create table abc_db.lob_custom_fieldswith foreign keyfk_section_id constraint failed. There is no index in the referenced table where the referenced columns appear as the first columns.------------".

如果我从 lob_custom_fields table 的 create table 语句中删除行“CONSTRAINT unq_section_field_label UNIQUE (section_id, field_label),”,则它创建成功。

如何在lob_custom_fieldstable中添加唯一索引?当我尝试添加唯一索引时,Create-Alter 都显示相同的错误。任何帮助将不胜感激。

错误消息说

Specified key was too long; max key length is 3072 bytes

The InnoDB internal maximum key length is 3500 bytes, but MySQL itself restricts this to 3072 bytes. This limit applies to the length of the combined index key in a multi-column index.

那是 mysql 8

所以你必须定义

field_label varchar(1022)

适合

CREATE TABLE `lob_sections` (
 `id` int(11) NOT NULL AUTO_INCREMENT,
 `section_name` varchar(600) NOT NULL,
 `lob_type` varchar(64) NOT NULL,
 `agency_id` varchar(64) NOT NULL,
 `display_order` tinyint(2) NOT NULL DEFAULT 1,
 `active` tinyint(1) NOT NULL DEFAULT 1,
 `created_date` timestamp NOT NULL DEFAULT current_timestamp(),
 `last_modified_date` timestamp NOT NULL DEFAULT current_timestamp() ON UPDATE current_timestamp(),
 PRIMARY KEY (`id`),
 UNIQUE KEY `unq_lob_sections` (`agency_id`,`lob_type`,`section_name`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
CREATE TABLE `lob_custom_fields` (
 `id` int(11) NOT NULL AUTO_INCREMENT,
 `section_id` int(11) NOT NULL,
 `field_label` varchar(1022) NOT NULL,
 `field_type` varchar(20) NOT NULL,
 `active` tinyint(1) NOT NULL DEFAULT 1,
 `display_order` tinyint(3) NOT NULL DEFAULT 1,
 `required` tinyint(1) NOT NULL DEFAULT 0,
 `created_date` timestamp NOT NULL DEFAULT current_timestamp(),
 `last_modified_date` timestamp NOT NULL DEFAULT current_timestamp() ON UPDATE current_timestamp(),
 PRIMARY KEY (`id`),
 CONSTRAINT unq_section_field_label UNIQUE (section_id, field_label),
 CONSTRAINT `fk_section_id` FOREIGN KEY (`section_id`) REFERENCES `lob_sections` (`id`) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

db<>fiddle here