Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Integrity constraint violation: 1452 Cannot add or update a child row:

Tags:

sql

mysql

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

-- ---------------------------- -- Table structure for `comments` -- ---------------------------- DROP TABLE IF EXISTS `comments`; CREATE TABLE `comments` (   `id` varchar(36) NOT NULL,   `project_id` varchar(36) NOT NULL,   `user_id` varchar(36) NOT NULL,   `task_id` varchar(36) NOT NULL,   `data_type_id` varchar(36) NOT NULL,   `data_path` varchar(255) DEFAULT NULL,   `message` longtext,   `created` datetime DEFAULT NULL,   `modified` datetime DEFAULT NULL,   PRIMARY KEY (`id`),   KEY `fk_comments_users` (`user_id`),   KEY `fk_comments_projects1` (`project_id`),   KEY `fk_comments_data_types1` (`data_type_id`),   CONSTRAINT `fk_comments_data_types1` FOREIGN KEY (`data_type_id`) REFERENCES `data_types` (`id`) ON DELETE NO ACTION ON UPDATE NO ACTION,   CONSTRAINT `fk_comments_projects1` FOREIGN KEY (`project_id`) REFERENCES `projects` (`id`) ON DELETE NO ACTION ON UPDATE NO ACTION,   CONSTRAINT `fk_comments_users` FOREIGN KEY (`user_id`) REFERENCES `users` (`id`) ON DELETE NO ACTION ON UPDATE NO ACTION ) ENGINE=InnoDB DEFAULT CHARSET=utf32;  -- ---------------------------- -- Records of comments -- ----------------------------  -- ---------------------------- -- Table structure for `projects` -- ---------------------------- DROP TABLE IF EXISTS `projects`; CREATE TABLE `projects` (   `id` varchar(36) NOT NULL,   `user_id` varchar(36) NOT NULL,   `title` varchar(45) DEFAULT NULL,   `description` longtext,   `created` datetime DEFAULT NULL,   `modified` datetime DEFAULT NULL,   PRIMARY KEY (`id`),   KEY `fk_projects_users1` (`user_id`),   CONSTRAINT `fk_projects_users1` FOREIGN KEY (`user_id`) REFERENCES `users` (`id`) ON DELETE NO ACTION ON UPDATE NO ACTION ) ENGINE=InnoDB DEFAULT CHARSET=utf32;  -- ---------------------------- -- Records of projects -- ---------------------------- INSERT INTO `projects` VALUES ('50dcbc72-3410-4596-8b71-0e80ae7aaee3', '50dcbc5c-d684-40bf-9715-0becae7aaee3', 'Brand New Project', 'This is a brand new project', '2012-12-27 15:24:02', '2012-12-27 15:24:02'); 

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

INSERT INTO `anthonyl_fbpj`.`comments` (`project_id`, `user_id`, `task_id`, `data_type_id`, `message`, `modified`, `created`, `id`)  VALUES ('50dc845a-83e4-4db3-8705-5432ae7aaee3', '50dcbc5c-d684-40bf-9715-0becae7aaee3', '1', '50d32e5c-abdc-491a-a0ef-25d84e9f49a8', 'this is a test', '2012-12-27 19:20:46', '2012-12-27 19:20:46', '50dcf3ee-8bf4-4685-aa45-4eb4ae7aaee3') 

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 (anthonyl_fbpj.comments, CONSTRAINT fk_comments_projects1 FOREIGN KEY (project_id) REFERENCES projects (id) ON DELETE NO ACTION ON UPDATE NO ACTION)

like image 873
numerical25 Avatar asked Dec 28 '12 01:12

numerical25


People also ask

How do you fix Integrity constraint violation 1452 Cannot add or update a child row a foreign key constraint fails?

If you are getting error Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails while using php artisan migrate command after creating a migration file. You can set foreign id column value as nullable with null on delete on delete parent table record.

How do you correct an integrity constraint violation?

In order to remedy this error, you will need to insert the value that you attempted to place in the child table into the parent table first. Once inserted as a parent row, you can go back and insert the value into the child table.

Why Cannot add or update a child row?

The error comes when you are trying to add a row for which no matching row in in the other table. “Foreign key relationships involve a parent table that holds the central data values, and a child table with identical values pointing back to its parent. The FOREIGN KEY clause is specified in the child table.

What is integrity constraint violation?

Integrity constraint violations occur when an insert, update, or delete statement violates a primary key, foreign key, check, or unique constraint or a unique index. Message. Value. Description. Attempt to insert duplicate key row in object <object_name> with unique index <index_name> .


2 Answers

It just simply means that the value for column project_id on table comments you are inserting doesn't exist on table projects. Bear in mind that the values of column project_id on table comments is dependent on the values of ID on table Projects.

The value 50dc845a-83e4-4db3-8705-5432ae7aaee3 you are inserting for column project_id does not exist on table projects.

like image 116
John Woo Avatar answered Sep 25 '22 10:09

John Woo


Make sure you have project_id in the fillable property of your Comment model.

I had the same issue, And this was the reason.

like image 44
Jyothu Avatar answered Sep 25 '22 10:09

Jyothu