title: Error in mysql - ERROR 1215 (HY000): Cannot add foreign key constraint
body: <p>I want to create table with two foreign key column reference to ID and stocks of Product Detail table</p>
  <p>Reference table Product Detail:</p>
  <pre><code>mysql&gt; describe productDetail;
  +--------------+--------------+------+-----+-------------------+-----------------------------+
  | Field        | Type         | Null | Key | Default           | Extra                       |
  +--------------+--------------+------+-----+-------------------+-----------------------------+
  | ProductName  | varchar(255) | YES  |     | NULL              |                             
  |
  | productPrice | double       | YES  |     | NULL              |                             |
  | GST          | int(25)      | YES  |     | NULL              |                             |
  | stocks       | int(11)      | NO   | PRI | NULL              |                             |
  | date         | timestamp    | NO   |     | CURRENT_TIMESTAMP | on update CURRENT_TIMESTAMP |
  | ID           | int(11)      | NO   | PRI | NULL              | auto_increment              |
  +--------------+--------------+------+-----+-------------------+-----------------------------+
  6 rows in set (0.00 sec)
  </code></pre>
  <p>Table Where i have to set foreign key :</p>
  <pre><code>mysql&gt; describe TodayProduction;
  +-----------+---------+------+-----+---------+-------+
  | Field     | Type    | Null | Key | Default | Extra |
  +-----------+---------+------+-----+---------+-------+
  | TP_ID     | int(11) | NO   | MUL | NULL    |       |
  | TP_Stocks | int(11) | NO   |     | NULL    |       |
  +-----------+---------+------+-----+---------+-------+
  2 rows in set (0.00 sec)
  </code></pre>
  <p>I have successfully add foreign key to the TP_ID reference to ID of productDetail
  but i cant add foreign key to the TP_Stocks reference to stocks column of productDetail</p>
  <p><strong>Query to add foreign key</strong></p>
  <pre><code>alter table TodayProduction add constraint fk_Stocks foreign key (TP_Stocks) references productDetail(stocks);
  </code></pre>
  <p><strong>Error</strong></p>
  <pre><code>ERROR 1215 (HY000): Cannot add foreign key constraint
  </code></pre>
  <p><strong>SHOW ENGINE INNODB STATUS;</strong></p>
  <pre><code>Cannot find an index in the referenced table where the
  referenced columns appear as the first columns, or column types
  in the table and the referenced table do not match for constraint.
  Note that the internal storage type of ENUM and SET changed in
  tables created with &gt;= InnoDB-4.1.12, and such columns in old tables
  cannot be referenced by such columns in new tables.
  </code></pre>