我正在创建一些新表,我想用测试数据填充它们,而我在mysql 8中遇到了这个问题。
我总是在表上使用空FK,但现在我不知道发生了什么。
我认为是来自mysql 8的东西,我最近更新了,到现在为止我还没有问题。
我正在使用8.0.12。
我得到的错误是:
[23000] [1452]无法添加或更新子行:外键 约束失败(
i2cwac_test
。site_board_pins
,约束site_board_pins_sensor_types_id_fk
外键(id
)参考sensor_types
(id
))
表创建脚本:
CREATE TABLE site_board_pins
(
id bigint(20) PRIMARY KEY NOT NULL AUTO_INCREMENT,
site_board_id bigint(20) NOT NULL,
e_board_pin_id bigint(20) NOT NULL,
pin_type_id bigint(20) NOT NULL,
pin_operation_mode bigint(20) NULL,
sensor_type_id bigint(20) NULL,
enabled bit(1) NOT NULL ,
description varchar(500),
CONSTRAINT site_board_pins_site_boards_id_fk FOREIGN KEY (id) REFERENCES site_boards (id),
CONSTRAINT site_board_pins_e_board_pins_id_fk FOREIGN KEY (id) REFERENCES e_board_pins (id),
CONSTRAINT site_board_pins_pin_types_id_fk FOREIGN KEY (id) REFERENCES pin_types (id),
CONSTRAINT site_board_pins_pin_operation_mode_id_fk FOREIGN KEY (id) REFERENCES pin_operation_mode (id),
CONSTRAINT site_board_pins_sensor_types_id_fk FOREIGN KEY (id) REFERENCES sensor_types (id)
);
插入失败:
INSERT INTO `site_board_pins` (`site_board_id`, `e_board_pin_id`, `pin_type_id`, `pin_operation_mode`, `sensor_type_id`, `enabled`)
VALUES
((select id from e_boards where name = 'Iboard Pro 1.1'),
(select ep.id from e_boards eb join e_board_pins ep on ep.e_board_id = eb.id where eb.name = 'Iboard Pro 1.1' and ep.name = 'A1'),
(select id from pin_types where mode = 'Analog'),
NULL,
NULL,
0);
预先感谢
答案 0 :(得分:1)
看起来您搞砸了FK定义-所有FK都声明您的Id
列存在于所有其他表中。
例如,我很确定您的第一个应该是:
CONSTRAINT site_board_pins_site_boards_id_fk FOREIGN KEY (site_board_id) REFERENCES site_boards (id)