title: MySQL - Why can't I add a foreign key here?
body: <p>I'm not sure why I'm not able to add a particular foreign key here.  These tables were generated via MySQL Workbench, and based on the MySQL documentation and on searching for other similar problems / solutions, I <em>think</em> everything is in order...yet I can't add one foreign key in particular:</p>
  
  <p><code>inspection_statuses</code>.<code>inspection_id</code> needs to reference <code>inspection_responses</code>.<code>tpa_result</code></p>
  
  <p>What am I missing / overlooking?</p>
  
  <p>This is the statement I'm trying to use to add the new foreign key:</p>
  
  <pre><code>ALTER TABLE `vipsouth_app`.`inspection_statuses` 
  ADD CONSTRAINT `inspection_statuses_ibfk_3`
    FOREIGN KEY (`inspection_id`)
    REFERENCES `vipsouth_app`.`inspection_responses` (`tpa_result`)
    ON DELETE NO ACTION
    ON UPDATE NO ACTION;
  </code></pre>
  
  <p>And that produces this error:</p>
  
  <p>Operation failed: There was an error while applying the SQL script to the database.
  ERROR 1005: Can't create table <code>vipsouth_app</code>.<code>#sql-1f48_7</code> (errno: 150 "Foreign key constraint is incorrectly formed")</p>
  
  <pre><code>CREATE TABLE `inspection_responses` (
    `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
    `inspection_request_id` int(10) unsigned NOT NULL,
    `tpa_result` varchar(10) CHARACTER SET utf8 NOT NULL,
    `created_at` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',
    `created_by` int(10) unsigned NOT NULL DEFAULT '0',
    `updated_at` timestamp NULL DEFAULT NULL,
    `updated_by` int(10) unsigned DEFAULT NULL,
    `deleted_at` timestamp NULL DEFAULT NULL,
    `deleted_by` int(10) unsigned DEFAULT NULL,
    PRIMARY KEY (`id`),
    UNIQUE KEY `id_UNIQUE` (`id`),
    UNIQUE KEY `tpa_result_UNIQUE` (`tpa_result`),
    KEY `inspection_responses_ibfk_1_idx` (`inspection_request_id`),
    CONSTRAINT `inspection_responses_ibfk_1` FOREIGN KEY (`inspection_request_id`) REFERENCES `inspection_requests` (`id`) ON DELETE     CASCADE ON UPDATE CASCADE
  ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci
  
  CREATE TABLE `inspection_statuses` (
    `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
    `inspection_request_id` int(10) unsigned NOT NULL,
    `inspection_response_id` int(10) unsigned NOT NULL,
    `tpa_code` varchar(4) COLLATE utf8_unicode_ci NOT NULL,
    `user_id` varchar(30) COLLATE utf8_unicode_ci NOT NULL,
    `password` varchar(100) COLLATE utf8_unicode_ci NOT NULL,
    `inspection_id` varchar(10) COLLATE utf8_unicode_ci NOT NULL,
    `status` varchar(50) COLLATE utf8_unicode_ci DEFAULT NULL,
    `note` varchar(1000) COLLATE utf8_unicode_ci DEFAULT NULL,
    `url` varchar(1024) COLLATE utf8_unicode_ci DEFAULT NULL,
    `created_at` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',
    `created_by` int(10) unsigned NOT NULL,
    `updated_at` timestamp NULL DEFAULT NULL,
    `updated_by` int(10) unsigned DEFAULT NULL,
    `deleted_at` timestamp NULL DEFAULT NULL,
    `deleted_by` int(10) unsigned DEFAULT NULL,
    PRIMARY KEY (`id`),
    UNIQUE KEY `id_UNIQUE` (`id`),
    KEY `inspection_statuses_ibfk_1_idx` (`inspection_request_id`),
    KEY `inspection_statuses_ibfk_2_idx` (`inspection_response_id`),
    CONSTRAINT `inspection_statuses_ibfk_1` FOREIGN KEY (`inspection_request_id`) REFERENCES `inspection_requests` (`id`) ON DELETE CASCADE ON UPDATE CASCADE,
    CONSTRAINT `inspection_statuses_ibfk_2` FOREIGN KEY (`inspection_response_id`) REFERENCES `inspection_responses` (`id`) ON DELETE CASCADE ON UPDATE CASCADE
  ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci
  </code></pre>