2016-04-26 61 views
0

我不知道爲什麼我無法在這裏添加特定的外鍵。這些表格是通過MySQL Workbench生成的,並且基於MySQL文檔和尋找其他類似的問題/解決方案,我認爲一切都是爲了......但我無法添加一個特別的外鍵: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 
+0

你能提供MySQL錯誤消息嗎? \t 這可能是因爲類型不一樣。您試圖用'tpa_result'('varchar(10)CHARACTER SET utf8 NOT NULL')映射'COLLATE'('inspection_id varchar(10)COLLATE utf8_unicode_ci NOT NULL') – Delphine

+0

我更新了一些更詳細的問題,包括我收到的錯誤消息。 – CJG

+0

您是否嘗試將同一個類型同時寫入'inspection_id'和'tpa_result'?確保在'tpa_result'上啓用了'UNIQUE'約束條件。 – Delphine

回答

0

The data types and constraints defined on both the columns should be exactly the same. Except for a foreign key can be 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; 
+0

如果我禁用外鍵檢查,我仍然收到錯誤: 錯誤1822:無法添加外鍵約束。在參考表'inspection_responses'中缺少約束'inspection_statuses_ibfk_3'的索引 – CJG

+0

@CJG - 這對我來說工作得很好。 – MontyPython

+0

我也可以運行這2個'CREATE TABLE'語句,沒有錯誤;問題是添加另一個外鍵。我更新了我的問題以包含導致錯誤的'ALTER TABLE'語句。對不起,當我最初發布這個問題時,我還不夠具體。 – CJG