gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 21:05:24 26 4
gpt4 key购买 nike

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

inspection_statuses.inspection_id需要引用inspection_responses.tpa_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(errno: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

最佳答案

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;

关于MySQL - 为什么我不能在这里添加外键?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36859987/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com