0

I'm trying to move data from one table to another, but having an error:

mysql> INSERT INTO activities_article (category_id, created_at, title, promo, content, promo_image_url, coordinates, vk_id, updated_at, hot)
SELECT category_id, created_at, title, promo, content, promo_image_url, coordinates, vk_id, NOW(), False 
FROM dorogirussia.activities_article WHERE category_id=4;

ERROR 1452 (23000): Cannot add or update a child row: a foreign key constraint fails (`dorogi`.`activities_article`, CONSTRAINT `activities_articl_category_id_632c9272_fk_activities_category_id` FOREIGN KEY (`category_id`) REFERENCES `activities_category` (`id`))

Is there any way to fix that?

  • I take it you've read the error message and checked to see if there's a given row in activities_category with a relevant id ? – Philᵀᴹ Jul 13 '16 at 6:50
  • I have a row with id=4 in activities_category – Viktor Jul 13 '16 at 6:52
  • In fact, my question was stupid, seeing as it's a INSERT INTO ... SELECT ... FROM the same table – Philᵀᴹ Jul 13 '16 at 6:53
  • It's not the exact same table. I move data from one database to another – Viktor Jul 13 '16 at 6:55
1

Please make sure you have the value "4" in "id" column in dorogi.activities_category and also change the query to the following:

INSERT INTO dorogi.activities_article (category_id, created_at, title, promo, content, promo_image_url, coordinates, vk_id, updated_at, hot)
SELECT category_id, created_at, title, promo, content, promo_image_url, coordinates, vk_id, NOW(), False 
FROM dorogirussia.activities_article WHERE category_id=4;

Hope this solves your problem !!

|improve this answer|||||

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.