禁用时mysql外键约束失败

时间:2018-04-13 19:57:57

标签: mysql foreign-keys

我在从转储中将数据恢复到mysql数据库时遇到问题。我正在运行相同版本的mysql,我已禁用外键检查,数据类型相同,但我无法理解检查失败的原因

Error in foreign key constraint of table staging/bookmarks:
 FOREIGN KEY (`user_id`) REFERENCES `users` (`id`) ON DELETE CASCADE ON UPDATE CASCADE,
  CONSTRAINT `bookmarks_video_id_foreign` FOREIGN KEY (`video_id`) REFERENCES `videos` (`id`) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci:
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.

表架构

-- Server version   5.6.34-log

/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;
/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;
/*!40101 SET NAMES utf8 */;
/*!40103 SET @OLD_TIME_ZONE=@@TIME_ZONE */;
/*!40103 SET TIME_ZONE='+00:00' */;
/*!40014 SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0 */;
/*!40014 SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0 */;
/*!40101 SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='NO_AUTO_VALUE_ON_ZERO' */;
/*!40111 SET @OLD_SQL_NOTES=@@SQL_NOTES, SQL_NOTES=0 */;

...

--
-- Table structure for table `bookmarks`
--

DROP TABLE IF EXISTS `bookmarks`;
/*!40101 SET @saved_cs_client     = @@character_set_client */;
/*!40101 SET character_set_client = utf8 */;
CREATE TABLE `bookmarks` (
  `id` varchar(36) COLLATE utf8_unicode_ci NOT NULL,
  `user_id` varchar(36) COLLATE utf8_unicode_ci NOT NULL,
  `video_id` varchar(36) COLLATE utf8_unicode_ci NOT NULL,
  `course_id` varchar(36) COLLATE utf8_unicode_ci NOT NULL,
  `time` int(11) NOT NULL,
  `title` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
  `created_at` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',
  `updated_at` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',
  `deleted_at` timestamp NULL DEFAULT NULL,
  `notes` text COLLATE utf8_unicode_ci,
  PRIMARY KEY (`id`),
  UNIQUE KEY `bookmarks_course_id_user_id_video_id_time_unique` (`course_id`,`user_id`,`video_id`,`time`),
  KEY `bookmarks_user_id_foreign` (`user_id`),
  KEY `bookmarks_video_id_foreign` (`video_id`),
  CONSTRAINT `bookmarks_course_id_foreign` FOREIGN KEY (`course_id`) REFERENCES `courses` (`id`) ON DELETE CASCADE ON UPDATE CASCADE,
  CONSTRAINT `bookmarks_user_id_foreign` FOREIGN KEY (`user_id`) REFERENCES `users` (`id`) ON DELETE CASCADE ON UPDATE CASCADE,
  CONSTRAINT `bookmarks_video_id_foreign` FOREIGN KEY (`video_id`) REFERENCES `videos` (`id`) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
/*!40101 SET character_set_client = @saved_cs_client */;


...


--
-- Table structure for table `users`
--

DROP TABLE IF EXISTS `users`;
/*!40101 SET @saved_cs_client     = @@character_set_client */;
/*!40101 SET character_set_client = utf8 */;
CREATE TABLE `users` (
  `id` varchar(36) COLLATE utf8_unicode_ci NOT NULL,
  `username` varchar(63) COLLATE utf8_unicode_ci DEFAULT NULL,
  `password` varchar(60) COLLATE utf8_unicode_ci DEFAULT NULL,
  `email` varchar(63) COLLATE utf8_unicode_ci NOT NULL,
  `role_id` varchar(36) COLLATE utf8_unicode_ci DEFAULT NULL,
  `last_login` datetime DEFAULT NULL,
  `customer_id` int(11) DEFAULT NULL,
  `hours_watched` int(11) NOT NULL DEFAULT '0',
  `points` int(11) NOT NULL DEFAULT '0',
  `created_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
  `updated_at` timestamp NULL DEFAULT NULL,
  `deleted_at` timestamp NULL DEFAULT NULL,
  `remember_token` varchar(100) COLLATE utf8_unicode_ci DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
/*!40101 SET character_set_client = @saved_cs_client */;


....


--
-- Table structure for table `videos`
--

DROP TABLE IF EXISTS `videos`;
/*!40101 SET @saved_cs_client     = @@character_set_client */;
/*!40101 SET character_set_client = utf8 */;
CREATE TABLE `videos` (
  `id` varchar(36) COLLATE utf8_unicode_ci NOT NULL,
  `title` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
  `is_public` smallint(6) NOT NULL DEFAULT '0',
  `is_visible` smallint(6) NOT NULL DEFAULT '0',
  `points` int(11) NOT NULL DEFAULT '0',
  `duration` varchar(10) COLLATE utf8_unicode_ci NOT NULL,
  `provider_id` varchar(36) COLLATE utf8_unicode_ci NOT NULL,
  `presenter_id` varchar(36) COLLATE utf8_unicode_ci NOT NULL,
  `level_id` varchar(36) COLLATE utf8_unicode_ci NOT NULL,
  `hd_url` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
  `high_url` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
  `mobile_url` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
  `transcript_file` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
  `created_at` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',
  `updated_at` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',
  `deleted_at` timestamp NULL DEFAULT NULL,
  `order` int(11) NOT NULL DEFAULT '0',
  `prefix_title` varchar(20) COLLATE utf8_unicode_ci DEFAULT NULL,
  `jwplayer_mediaid` varchar(16) COLLATE utf8_unicode_ci DEFAULT NULL,
  PRIMARY KEY (`id`),
  KEY `videos_level_id_foreign` (`level_id`),
  KEY `videos_provider_id_foreign` (`provider_id`),
  KEY `videos_presenter_id_foreign` (`presenter_id`),
  CONSTRAINT `videos_level_id_foreign` FOREIGN KEY (`level_id`) REFERENCES `levels` (`id`) ON DELETE CASCADE ON UPDATE CASCADE,
  CONSTRAINT `videos_presenter_id_foreign` FOREIGN KEY (`presenter_id`) REFERENCES `presenters` (`id`) ON UPDATE CASCADE,
  CONSTRAINT `videos_provider_id_foreign` FOREIGN KEY (`provider_id`) REFERENCES `providers` (`id`) ON UPDATE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
/*!40101 SET character_set_client = @saved_cs_client */;


...

/*!40014 SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS */;
/*!40014 SET UNIQUE_CHECKS=@OLD_UNIQUE_CHECKS */;

1 个答案:

答案 0 :(得分:0)

您必须先定义要引用的表(users),然后才能向其声明外键。

在您的情况下,您要在另一个表bookmarks存在之前创建表users

您获得的错误与foreign_key_checks被禁用与启用无关。

实际上,我必须纠正我的答案。这个转储文件应该可以正常工作,并且在我在本地MySQL 5.6实例中测试它时可以正常工作。

解决外键排序问题的方法是:

SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0;

这将禁用外键规则的强制执行,直到最后恢复强制执行:

SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS;

我可以建议的是,...空间中的某些内容正在过早恢复外键执行。您需要检查完整的文件并找出答案。