ERROR 1452: Cannot add or update a child row: a foreign key constraint fails

跟風遠走 提交于 2019-11-27 07:50:40

问题


I have created two tables in MySQL 5.6.11 as shown below by means of MySQL Workbench 5.2.47.

The country table:

delimiter $$

CREATE TABLE `country` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `country_name` varchar(45) DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INC

REMENT=2 DEFAULT CHARSET=utf8$$

The state_table:

delimiter $$

CREATE TABLE `state_table` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `state_name` varchar(45) DEFAULT NULL,
  `country_id` int(11) DEFAULT NULL,
  PRIMARY KEY (`id`),
  CONSTRAINT `country_fk` FOREIGN KEY (`id`) REFERENCES `country` (`id`) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8 COMMENT=''$$

There is one row in the country table with the id 1. It allows only one (child) row to be inserted into its child table state_table. If more rows are attempted, then the following error occurs.

ERROR 1452: Cannot add or update a child row: a foreign key constraint fails (social_networking.state_table, CONSTRAINT country_fk FOREIGN KEY (id) REFERENCES country (id) ON DELETE CASCADE ON UPDATE CASCADE)

SQL Statement:

INSERT INTO `social_networking`.`state_table` (`id`, `state_name`, `country_id`) VALUES ('2', 'xxx', '1')

Actually, I'm trying to map these tables using an ORM (JPA) where I always see only OneToOne relationship.

What am I missing?


回答1:


I think you have a typo in your foreign key constraint, country_id should probaby be the foreign key to country. When id is the foreign key, you can only insert one row since it just happens to get id=1 which is the same id as the row in country;

CONSTRAINT `country_fk` FOREIGN KEY (`id`) 
    REFERENCES `country` (`id`) ON DELETE CASCADE ON UPDATE CASCADE

should probably be

CONSTRAINT `country_fk` FOREIGN KEY (`country_id`) 
    REFERENCES `country` (`id`) ON DELETE CASCADE ON UPDATE CASCADE

An SQLfiddle to test with.




回答2:


Well, I find the answer, the solution, my english is not very well but I think can explain you. I get this error after try to create a trigger, My database was created in phpmyadmin, and this error was make me crazy, the problem was that I before create the trigger, I load a lot of data in my tables, and in my child table was some data that have no match in my parent table, ej: my child table "chat" have a "id_jugador=1" and in my parent table there wasn't that "id_jugador", that was my mistake, I hope help you, Argentina Rulz ;)




回答3:


I had the same problem and it was not wrong relationships name.
I had problem with different records, ie, tried registering a record that did not exist in another table so generated this error.
Check if the record exists in another table to insert their correct relationship, otherwise, this error appears.

Hope this help you.




回答4:


example:
 table1(
    id1 INT PRIMARY KEY,
    name1 VARCHAR(50)
 )
 table2(
    id2,<--- want to add FOREIGN KEY to this field
    name2 VARCHAR(50)
 )

Before adding constraint foreign key you must have the right value between id1 and id2, so you should update that id field with the value that map each other.




回答5:


Possible Solution

if you have live data, then check all column values.

i.e. if you have 'x'->table as primary one having 'a'->column and 'y'->table as secondary with 'b'->column, then all values in 'b'->column must exist in 'a'->column if any value that exists in 'b'->column and not exist in 'a'->column then it will give as such error..

Hope this help.. for newbie..



来源:https://stackoverflow.com/questions/16447137/error-1452-cannot-add-or-update-a-child-row-a-foreign-key-constraint-fails

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!