title: MySQL "Cannot add or update a child row: a foreign key constraint fails"
body: <p>I'm new to MySQL and databases in general.  I've been tasked with manually moving an old database to a new one of a slightly different format.  The challenges include transferring certain columns from a table in one database to another database of a similar format.  This is made further difficult in that the source database is MyISAM and the destination is InnoDB.</p>
  
  <p>So I have two databases, A is the source and B is the destination, and am attempting to copy 'most' of a table to a similar table in the destination database.<br>
  Here is the command I run:</p>
  
  <pre><code>INSERT INTO B.article (id, ticket_id, article_type_id,   
    article_sender_type_id, a_from, a_reply_to, a_to, a_cc, a_subject,
    a_message_id, a_in_reply_to, a_references, a_content_type, a_body,
    incoming_time, content_path, valid_id, create_time, create_by,change_time,
    change_by)
  SELECT id, ticket_id, article_type_id, article_sender_type_id,
    a_from, a_reply_to, a_to, a_cc, a_subject, a_message_id, a_in_reply_to,
    a_references, a_content_type, a_body, incoming_time, content_path,  
    valid_id, create_time, create_by, change_time, change_by 
  FROM A.article 
  WHERE id NOT IN ( 1 );
  </code></pre>
  
  <p>Error:</p>
  
  <pre><code>ERROR 1452 (23000): Cannot add or update a child row: a foreign key constraint fails (`helpdesk`.`article`, CONSTRAINT `FK_article_ticket_id_id` FOREIGN KEY (`ticket_id`) REFERENCES `ticket` (`id`))
  </code></pre>
  
  <p>The reason for making the command so wordy is that the source has several columns that were unnecessary and so were pruned out of the destination table.  The WHERE id NOT IN ( 1 ) is there so that the first row is not copied (it was initialized in both databases and MySQL throws an error if they both have the same 'id' field).  I can't tell by the error if it expects 'ticket_id' to be unique between rows, which it is not, or if it is claiming that a row does not have a ticket_id and so can not be copied which is what the error seems to most often be generated by.</p>
  
  <p>I can post the tables in question if that will help answer, but I am unsure of the best way to do that, so some pointing in the right direction there would be helpful as well.</p>
  
  <p>Posts I looked at before:</p>
  
  <p>For <a href="https://stackoverflow.com/questions/1353733/mysql-how-to-copy-an-entire-row-from-one-table-to-another-in-mysql-with-the-sec">forming</a> the <a href="https://stackoverflow.com/questions/14628269/error-code-1062-duplicate-entry-1-for-key-primary">command</a></p>
  
  <p>For <a href="https://stackoverflow.com/questions/5005388/cannot-add-or-update-a-child-row-a-foreign-key-constraint-fails">looking</a> at this <a href="https://stackoverflow.com/questions/21659691/error-1452-cannot-add-or-update-a-child-row-a-foreign-key-constraint-fails">error</a></p>
  
  <p>Thanks!</p>