MySQL - 为什么我不能在这里添加外键?

时间:2016-04-26 08:55:40

标签: mysql foreign-keys

我不知道为什么我无法在这里添加特定的外键。这些表是通过MySQL Workbench生成的,基于MySQL文档和搜索其他类似的问题/解决方案,我认为一切都井然有序......但我不能添加一个外键特别是:

inspection_statusesinspection_id需要引用inspection_responsestpa_result

我缺少什么/忽视?

这是我试图用来添加新外键的语句:

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;

这就产生了这个错误:

操作失败:将SQL脚本应用于数据库时出错。 错误1005:无法创建表vipsouth_app#sql-1f48_7(错误:150“外键约束形成错误”)

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

1 个答案:

答案 0 :(得分:0)

  

两列上定义的数据类型和约束应完全相同。除了外键可以是NULLable。

这应该为你做。我没有收到任何错误。

SET foreign_key_checks = 0;

    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;

SET foreign_key_checks = 1;