title: Adding Foreign Key to Existing Tables
body: <p>I have a parent <code>orders</code> table like this:</p>
  
  <pre><code>+---------------+---------------+------+-----+-----------+----------------+
  | Field         | Type          | Null | Key | Default   | Extra          |
  +---------------+---------------+------+-----+-----------+----------------+
  | id            | int(11)       | NO   | PRI | NULL      | auto_increment |
  | pnref         | varchar(50)   | YES  | UNI | NULL      |                |
  | customerid    | int(11)       | YES  |     | NULL      |                |
  +-------------------------------------------------------------------------+
  </code></pre>
  
  <p>I have a child <code>orders_content</code> table like this:</p>
  
  <pre><code>+------------------+--------------+------+-----+---------+----------------+
  | Field            | Type         | Null | Key | Default | Extra          |
  +------------------+--------------+------+-----+---------+----------------+
  | id               | int(6)       | NO   | PRI | NULL    | auto_increment |
  | orderid          | int(11)      | NO   |     | NULL    |                |
  | status           | varchar(50)  | YES  |     | pending |                |
  +------------------+--------------+------+-----+---------+----------------+
  </code></pre>
  
  <p>I am attempting to add a<code>FOREIGN KEY</code> like this:</p>
  
  <pre><code>ALTER TABLE orders_content 
  ADD CONSTRAINT fk_orders_content 
  FOREIGN KEY(orderid) REFERENCES orders(id)
  ON DELETE CASCADE ON UPDATE CASCADE;
  </code></pre>
  
  <p>I cannot resolve why this fails:</p>
  
  <p><code>1452 - Cannot add or update a child row: a foreign key constraint fails (</code>gls<code>.</code>#sql-1744_4d58<code>, CONSTRAINT</code>fk_orders_content<code>FOREIGN KEY (</code>orderid<code>) REFERENCES</code>orders<code>(</code>id<code>) ON DELETE CASCADE ON UPDATE CASCADE)</code></p>
  
  <p>If anyone can see my error or errors I would appreciate it.</p>