简体   繁体   中英

MySQL 8 NULL foreign key

I am creating some new tables and i want tot populate them with data for tests and i got into this problem with mysql 8.

I always worked with null FK on tables but now i don't know what is happening.

I think is something from mysql 8, i updated recently and i didn't have problems with it till now.

I am using 8.0.12.

error that i get is :

[23000][1452] Cannot add or update a child row: a foreign key constraint fails ( i2cwac_test . site_board_pins , CONSTRAINT site_board_pins_sensor_types_id_fk FOREIGN KEY ( id ) REFERENCES sensor_types ( id ))

table creation script:

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 that fails :

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);

Thanks in advance

Looks like you've messed up your FK definitions - all the FKs are declared say that your Id column exists in all these other tables.

For example, I'm pretty sure your first one is meant to be:

CONSTRAINT site_board_pins_site_boards_id_fk FOREIGN KEY (site_board_id) REFERENCES site_boards (id)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM