模式是如下圖所示插入到子表有外鍵AUTO_INCREMENT列
CREATE TABLE `country` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`country_name` varchar(45) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8;
CREATE TABLE `state_table` (
`id` int(11),
`state_name` varchar(45) DEFAULT NULL,
`country_id` int(11) NOT NULL AUTO_INCREMENT,
PRIMARY KEY (`country_id`),
CONSTRAINT `country_fk` FOREIGN KEY (`country_id`)
REFERENCES `country` (`id`) ON DELETE CASCADE on update cascade
) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8 COMMENT='';
當我試圖將數據插入到兩個表,將數據插入到父表,但它不插入在這裏的子表和錯誤是
Schema Creation Failed: Cannot add or update a child row: a foreign key constraint fails (`db_2_3b712`.`state_table`, CONSTRAINT `country_fk` FOREIGN KEY (`country_id`) REFERENCES `country` (`id`) ON DELETE CASCADE ON UPDATE CASCADE):
根據我的想法有country_id作爲auto_increment創建問題。
我試圖插入這樣
INSERT INTO country (country_name) VALUES ('US of A');
INSERT INTO state_table (state_name,id) VALUES
('Minnesota', 2),
('Arizona', 2);
幫助我。