简体   繁体   中英

Unable to create table with foreign key constraint

This is the table structure I am trying to create:

CREATE TABLE `user_like_dislike_blog_comments` (
  `id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
  `user_id` bigint(20) unsigned NOT NULL,
  `blog_id` bigint(20) unsigned NOT NULL,
  `blog_comment_id` bigint(20) unsigned NOT NULL,
  `is_disliked` bigint(20) NOT NULL,
  `is_liked` bigint(20) NOT NULL,
  `created_by` bigint(20) unsigned NOT NULL,
  `updated_by` bigint(20) unsigned DEFAULT NULL,
  `created_at` timestamp NULL DEFAULT NULL,
  `updated_at` timestamp NULL DEFAULT NULL,
  PRIMARY KEY (`id`),
  KEY `user_like_dislike_blog_comments_created_by_foreign` (`created_by`),
  KEY `user_like_dislike_blog_comments_updated_by_foreign` (`updated_by`),
  KEY `user_like_dislike_blog_comments_user_id_foreign` (`user_id`),
  KEY `user_like_dislike_blog_comments_blog_id_foreign` (`blog_id`),
  KEY `user_like_dislike_blog_comments_blog_comment_id_foreign` (`blog_comment_id`),
  CONSTRAINT `user_like_dislike_blog_comments_blog_comment_id_foreign` FOREIGN KEY (`blog_comment_id`) REFERENCES `blog_comments` (`id`),
  CONSTRAINT `user_like_dislike_blog_comments_blog_id_foreign` FOREIGN KEY (`blog_id`) REFERENCES `blogs` (`id`),
  CONSTRAINT `user_like_dislike_blog_comments_created_by_foreign` FOREIGN KEY (`created_by`) REFERENCES `users` (`id`),
  CONSTRAINT `user_like_dislike_blog_comments_updated_by_foreign` FOREIGN KEY (`updated_by`) REFERENCES `users` (`id`),
  CONSTRAINT `user_like_dislike_blog_comments_user_id_foreign` FOREIGN KEY (`user_id`) REFERENCES `users` (`id`) ON DELETE CASCADE
) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

But this error I am getting:

Can't create table unityofhindu1 . user_like_dislike_blog_comments (errno: 150 "Foreign key constraint is incorrectly formed")

Make sure your foreign-candidate key column type and size must be same in both the tables.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM