Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.6k views
in Technique[技术] by (71.8m points)

mysql - SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails

I am trying to insert values into my comments table and I am getting a error. Its saying that I can not add or update child row and I have no idea what that means. My schema looks something like this:

--
-- Baza danych: `koxu1996_test`
--

-- --------------------------------------------------------

--
-- Struktura tabeli dla tabeli `user`
--

CREATE TABLE IF NOT EXISTS `user` (
  `id` int(8) NOT NULL AUTO_INCREMENT,
  `username` varchar(32) COLLATE utf8_bin NOT NULL,
  `password` varchar(64) COLLATE utf8_bin NOT NULL,
  `password_real` char(32) COLLATE utf8_bin NOT NULL,
  `email` varchar(32) COLLATE utf8_bin NOT NULL,
  `code` char(8) COLLATE utf8_bin NOT NULL,
  `activated` enum('0','1') COLLATE utf8_bin NOT NULL DEFAULT '0',
  `activation_key` char(32) COLLATE utf8_bin NOT NULL,
  `reset_key` varchar(32) COLLATE utf8_bin NOT NULL,
  `name` varchar(32) COLLATE utf8_bin NOT NULL,
  `street` varchar(32) COLLATE utf8_bin NOT NULL,
  `house_number` varchar(32) COLLATE utf8_bin NOT NULL,
  `apartment_number` varchar(32) COLLATE utf8_bin NOT NULL,
  `city` varchar(32) COLLATE utf8_bin NOT NULL,
  `zip_code` varchar(32) COLLATE utf8_bin NOT NULL,
  `phone_number` varchar(16) COLLATE utf8_bin NOT NULL,
  `country` int(8) NOT NULL,
  `province` int(8) NOT NULL,
  `pesel` varchar(32) COLLATE utf8_bin NOT NULL,
  `register_time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
  `authorised_time` datetime NOT NULL,
  `edit_time` datetime NOT NULL,
  `saldo` decimal(9,2) NOT NULL,
  `referer_id` int(8) NOT NULL,
  `level` int(8) NOT NULL,
  PRIMARY KEY (`id`),
  KEY `country` (`country`),
  KEY `province` (`province`),
  KEY `referer_id` (`referer_id`)
) ENGINE=InnoDB  DEFAULT CHARSET=utf8 COLLATE=utf8_bin AUTO_INCREMENT=83 ;

and the mysql statement I am trying to do looks something like this:

INSERT INTO `user` (`password`, `code`, `activation_key`, `reset_key`, `register_time`,                `edit_time`, `saldo`, `referer_id`, `level`) VALUES (:yp0, :yp1, :yp2, :yp3, NOW(), NOW(), :yp4, :yp5, :yp6). Bound with :yp0='fa1269ea0d8c8723b5734305e48f7d46', :yp1='F154', :yp2='adc53c85bb2982e4b719470d3c247973', :yp3='', :yp4='0', :yp5=0, :yp6=1

the error I get looks like this:

SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails (koxu1996_test.user, CONSTRAINT user_ibfk_1 FOREIGN KEY (country) REFERENCES country_type (id) ON DELETE NO ACTION ON UPDATE NO ACTION)

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

It just simply means that the value for column country on table comments you are inserting doesn't exist on table country_type or you are not inserting value for country on table user. Bear in mind that the values of column country on table comments is dependent on the values of ID on table country_type.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...