插入子表时出现外键失败(引用复合唯一索引)

时间:2017-04-29 10:18:52

标签: mysql laravel

尝试在我的子数据库表中插入行时遇到问题。

这是我的父表:

CREATE TABLE `payables` (
  `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
  `type` varchar(191) COLLATE utf8mb4_unicode_ci NOT NULL,
  `currency_id` int(10) unsigned NOT NULL,
  PRIMARY KEY (`id`),
  UNIQUE KEY `payables_id_type_unique` (`id`,`type`),
  KEY `payables_currency_id_foreign` (`currency_id`),
  CONSTRAINT `payables_currency_id_foreign` FOREIGN KEY (`currency_id`) REFERENCES `currencies` (`id`) ON UPDATE CASCADE
) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

这是我的孩子表:

CREATE TABLE `pays` (
  `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
  `transaction_id` int(10) unsigned NOT NULL,
  `user_id` int(10) unsigned NOT NULL,
  `value` int(10) unsigned NOT NULL,
  `status_id` int(10) unsigned NOT NULL,
  `payable_id` int(10) unsigned NOT NULL,
  `payable_type` varchar(191) COLLATE utf8mb4_unicode_ci NOT NULL,
  `created_at` timestamp NULL DEFAULT NULL,
  `updated_at` timestamp NULL DEFAULT NULL,
  `deleted_at` timestamp NULL DEFAULT NULL,
  PRIMARY KEY (`id`),
  UNIQUE KEY `pays_transaction_id_unique` (`transaction_id`),
  KEY `pays_user_id_index` (`user_id`),
  KEY `pays_status_id_index` (`status_id`),
  KEY `pays_payable_id_index` (`payable_id`),
  KEY `pays_payable_type_index` (`payable_type`),
  KEY `pays_payable_id_payable_type_foreign` (`payable_id`,`payable_type`),
  CONSTRAINT `pays_payable_id_payable_type_foreign` FOREIGN KEY (`payable_id`, `payable_type`) REFERENCES `payables` (`id`, `type`) ON UPDATE CASCADE,
  CONSTRAINT `pays_status_id_foreign` FOREIGN KEY (`status_id`) REFERENCES `statuses` (`id`) ON UPDATE CASCADE,
  CONSTRAINT `pays_transaction_id_foreign` FOREIGN KEY (`transaction_id`) REFERENCES `transactions` (`id`) ON UPDATE CASCADE,
  CONSTRAINT `pays_user_id_foreign` FOREIGN KEY (`user_id`) REFERENCES `users` (`id`) ON UPDATE CASCADE
) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

这就是我试图播种的表格,父表已经播种了。

我正在使用laravel查询构建器来创建这些表,因此如果有更多数据库经验的人可以查看代码,那将非常感谢,谢谢。

1 个答案:

答案 0 :(得分:0)

很抱歉只是错过了我在我的专栏中插入了不同的数据类型。