Foreign key failure when inserting into child table ( referencing composite unique index) - mysql

I have problem when trying to insert row into my child database table.
Here is my parent table:
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;
And here is my child table:
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;
That is the table I'm trying to seed, the parent table is already seeded.
I'm using laravel query builder to create these tables, so if anyone more experienced with databases could review the code, that would be much appreciated, thanks.

Sorry just missed that i was inserting different data type to my column.

Related

foreign key relationship is not working in mysql

Here I am doing foreign key relation for my SQL, I am having 3 tables
product
gallery
offer
Here product table is a master table and gallery, offer tables are child table, now I trying to delete the master table entry but it not happening I am getting an error like
Cannot delete or update a parent row: a foreign key constraint fails
CREATE TABLE `product` (
`productId` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(100) COLLATE utf16_bin NOT NULL,
`price` decimal(10,0) NOT NULL,
`regOn` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
PRIMARY KEY (`productId`)
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf16 COLLATE=utf16_bin
CREATE TABLE `offer` (
`offerId` int(11) NOT NULL AUTO_INCREMENT,
`productId` int(11) NOT NULL,
`offerpercentage` text COLLATE utf16_bin NOT NULL,
PRIMARY KEY (`offerId`),
KEY `productId` (`productId`),
CONSTRAINT `offer_ibfk_1` FOREIGN KEY (`productId`) REFERENCES `product` (`productId`)
) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf16 COLLATE=utf16_bin
CREATE TABLE `gallery` (
`galleryId` int(11) NOT NULL AUTO_INCREMENT,
`productId` int(11) NOT NULL,
`galleryName` text COLLATE utf16_bin NOT NULL,
PRIMARY KEY (`galleryId`),
KEY `productId` (`productId`),
CONSTRAINT `gallery_ibfk_1` FOREIGN KEY (`productId`) REFERENCES `product` (`productId`)
) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf16 COLLATE=utf16_bin
Expected output
Suppose I am deleting master table entry means i have to delete all child tables entry also
You need to apply the ON DELETE CASCADE to your foreign key references.
CREATE TABLE `product` (
`productId` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(100) COLLATE utf16_bin NOT NULL,
`price` decimal(10,0) NOT NULL,
`regOn` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
PRIMARY KEY (`productId`)
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf16 COLLATE=utf16_bin
CREATE TABLE `offer` (
`offerId` int(11) NOT NULL AUTO_INCREMENT,
`productId` int(11) NOT NULL,
`offerpercentage` text COLLATE utf16_bin NOT NULL,
PRIMARY KEY (`offerId`),
KEY `productId` (`productId`),
CONSTRAINT `offer_ibfk_1` FOREIGN KEY (`productId`) REFERENCES `product` (`productId`) ON DELETE CASCADE
) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf16 COLLATE=utf16_bin
CREATE TABLE `gallery` (
`galleryId` int(11) NOT NULL AUTO_INCREMENT,
`productId` int(11) NOT NULL,
`galleryName` text COLLATE utf16_bin NOT NULL,
PRIMARY KEY (`galleryId`),
KEY `productId` (`productId`),
CONSTRAINT `gallery_ibfk_1` FOREIGN KEY (`productId`) REFERENCES `product` (`productId`) ON DELETE CASCADE
) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf16 COLLATE=utf16_bin
This tells MySQL that when the parent is deleted, delete the referencing child.

How to create database with foreign keys

I've got an issue when I try to fill my fixtures. I've got 2 tables (client and partner).
CREATE TABLE `partner` (
`id` tinyint(3) unsigned NOT NULL AUTO_INCREMENT,
`name` varchar(20) NOT NULL,
`client_id` int(10) unsigned NOT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `name` (`name`),
UNIQUE KEY `client_id` (`client_id`),
CONSTRAINT `partner_ibfk_3` FOREIGN KEY (`client_id`) REFERENCES `client`
(`id`)
) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8;
CREATE TABLE `client` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`partner_id` tinyint(3) unsigned DEFAULT NULL,
`partner_ref` varchar(7) DEFAULT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `partner_ref` (`partner_ref`,`partner_id`),
KEY `partner_id` (`partner_id`),
CONSTRAINT `client_ibfk_1` FOREIGN KEY (`partner_id`) REFERENCES `partner`
(`id`),
) ENGINE=InnoDB AUTO_INCREMENT=667305 DEFAULT CHARSET=utf8 PACK_KEYS=0;
When I run my fixtures I've got this issue :
ERROR 1215 (HY000) at line 863: Cannot add foreign key constraint
I supposed it's because when the table 'partner' is created, the table 'client' is not so the foreign key is not found.
However if I change and I put 'client' before 'partner', the problem it's same.
Thanks for your help !
I found a solution :
I removed the constraint in the creation of 'partner' and I added this constraint after the creation of 'client' with an ALTER TABLE :
CREATE TABLE `partner` (
`id` tinyint(3) unsigned NOT NULL AUTO_INCREMENT,
`name` varchar(20) NOT NULL,
`client_id` int(10) unsigned NOT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `name` (`name`),
UNIQUE KEY `client_id` (`client_id`)
) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8;
CREATE TABLE `client` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`partner_id` tinyint(3) unsigned DEFAULT NULL,
`partner_ref` varchar(7) DEFAULT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `partner_ref` (`partner_ref`,`partner_id`),
KEY `partner_id` (`partner_id`),
CONSTRAINT `client_ibfk_1` FOREIGN KEY (`partner_id`) REFERENCES `partner`
(`id`),
) ENGINE=InnoDB AUTO_INCREMENT=667305 DEFAULT CHARSET=utf8 PACK_KEYS=0;
ALTER TABLE `partner`
ADD CONSTRAINT `partner_ibfk_3` FOREIGN KEY (`client_id`) REFERENCES
`client` (`id`);

(errno: 150) Error Driving Me Nuts

I might be missing something here, I have given up after 2 weeks of trying, Can anyone maybe see something blindingly obvious why this will not import please ?
I have tried to check everything that I can think of but when I try to insert it I just get the 150 error message.
SET NAMES utf8;
SET time_zone = '+00:00';
SET foreign_key_checks = 0;
SET sql_mode = 'NO_AUTO_VALUE_ON_ZERO';
DROP TABLE IF EXISTS `classifieds_announcements`;
CREATE TABLE `classifieds_announcements` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`author` int(11) NOT NULL,
`category` int(11) NOT NULL,
`title` tinytext NOT NULL,
`text` text NOT NULL,
`date` int(11) NOT NULL COMMENT 'timestamp',
`expire` int(11) NOT NULL COMMENT 'timestamp',
`photo` varchar(255) NOT NULL,
`approved` int(11) NOT NULL DEFAULT '0',
PRIMARY KEY (`id`),
KEY `author` (`author`),
KEY `category` (`category`),
CONSTRAINT `classifieds_announcements_ibfk_1` FOREIGN KEY (`author`) REFERENCES `users` (`idu`) ON DELETE CASCADE,
CONSTRAINT `classifieds_announcements_ibfk_2` FOREIGN KEY (`category`) REFERENCES `classifieds_categories` (`id`) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
DROP TABLE IF EXISTS `classifieds_categories`;
CREATE TABLE `classifieds_categories` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` tinytext NOT NULL,
`parent` int(11) DEFAULT NULL,
`allow_posting` int(11) DEFAULT '1' COMMENT 'may be 0 or 1 only if parent=0, else will be 1',
`order` int(11) DEFAULT '50',
PRIMARY KEY (`id`),
KEY `parent` (`parent`),
CONSTRAINT `classifieds_categories_ibfk_1` FOREIGN KEY (`parent`) REFERENCES `classifieds_categories` (`id`) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
DROP TABLE IF EXISTS `classifieds_config`;
CREATE TABLE `classifieds_config` (
`key` varchar(255) NOT NULL,
`value` text NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
INSERT INTO `classifieds_config` (`key`, `value`) VALUES
('allow_emoticons', '1'),
('post_on_user_wall', '1'),
('approve_new_posts', '1'),
('index_columns', '4'),
('time_between_announces', '300'),
('time_between_reviews', '90');
DROP TABLE IF EXISTS `classifieds_inputs`;
CREATE TABLE `classifieds_inputs` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`category` int(11) NOT NULL,
`name` text NOT NULL,
`type` varchar(255) NOT NULL,
`options` text,
`required` int(11) DEFAULT NULL,
PRIMARY KEY (`id`),
KEY `category` (`category`),
CONSTRAINT `classifieds_inputs_ibfk_1` FOREIGN KEY (`category`) REFERENCES `classifieds_categories` (`id`) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
DROP TABLE IF EXISTS `classifieds_inputs_answers`;
CREATE TABLE `classifieds_inputs_answers` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`input` int(11) NOT NULL,
`announce` int(11) NOT NULL,
`answer` text NOT NULL,
PRIMARY KEY (`id`),
KEY `input` (`input`),
KEY `announce` (`announce`),
CONSTRAINT `classifieds_inputs_answers_ibfk_1` FOREIGN KEY (`input`) REFERENCES `classifieds_inputs` (`id`) ON DELETE CASCADE,
CONSTRAINT `classifieds_inputs_answers_ibfk_2` FOREIGN KEY (`input`) REFERENCES `classifieds_inputs` (`id`) ON DELETE CASCADE,
CONSTRAINT `classifieds_inputs_answers_ibfk_3` FOREIGN KEY (`announce`) REFERENCES `classifieds_announcements` (`id`) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
DROP TABLE IF EXISTS `classifieds_reports`;
CREATE TABLE `classifieds_reports` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`reported_by` int(11) NOT NULL,
`reported_announce` int(11) NOT NULL,
`read` int(11) NOT NULL DEFAULT '0',
PRIMARY KEY (`id`),
KEY `reported_by` (`reported_by`),
KEY `reported_announce` (`reported_announce`),
CONSTRAINT `classifieds_reports_ibfk_1` FOREIGN KEY (`reported_by`) REFERENCES `users` (`idu`) ON DELETE CASCADE,
CONSTRAINT `classifieds_reports_ibfk_2` FOREIGN KEY (`reported_announce`) REFERENCES `classifieds_announcements` (`id`) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
DROP TABLE IF EXISTS `classifieds_reviews`;
CREATE TABLE `classifieds_reviews` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`announce` int(11) NOT NULL,
`score` tinyint(4) NOT NULL,
`user` int(11) NOT NULL,
`text` text NOT NULL,
PRIMARY KEY (`id`),
KEY `announce` (`announce`),
KEY `user` (`user`),
CONSTRAINT `classifieds_reviews_ibfk_1` FOREIGN KEY (`announce`) REFERENCES `classifieds_announcements` (`id`) ON DELETE CASCADE,
CONSTRAINT `classifieds_reviews_ibfk_2` FOREIGN KEY (`user`) REFERENCES `users` (`idu`) ON DELETE CASCADE,
CONSTRAINT `classifieds_reviews_ibfk_3` FOREIGN KEY (`announce`) REFERENCES `classifieds_announcements` (`id`) ON DELETE CASCADE,
CONSTRAINT `classifieds_reviews_ibfk_4` FOREIGN KEY (`user`) REFERENCES `users` (`idu`) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

ERROR:: a foreign key constraint fails

Error::1452: Cannot add or update a child row: a foreign key constraint fails (findmybuffet.det_res_item_selected, CONSTRAINTfk_det_res_item_selected_Buf_Off_Id1FOREIGN KEY (Buf_Off_Id) REFERENCESmas_buf_off(Buf_Off_Id) ON DELETE CASCADE)
I have three tables below::
det_res_item_selected is throwing error when i tried to create the table
How can i resolve this ?
CREATE TABLE `det_res_item_selected` (
`Line_Selected_Item_Id` int(11) unsigned NOT NULL AUTO_INCREMENT,
`Item_Id` int(11) unsigned NOT NULL,
`Item_Selected` tinyint(4) DEFAULT '0',
`Buf_Off_Id` int(11) unsigned DEFAULT NULL,
`Item_Image` varchar(50) NOT NULL,
PRIMARY KEY (`Line_Selected_Item_Id`),
KEY `fk_det_res_item_selected_Item1_Idx` (`Item_Id`),
KEY `fk_det_res_item_selected_Buf_Off_Id1_Idx` (`Buf_Off_Id`),
CONSTRAINT `fk_det_res_item_selected_Buf_Off_Id1` FOREIGN KEY (`Buf_Off_Id`) REFERENCES `mas_buf_off` (`Buf_Off_Id`) ON DELETE CASCADE,
CONSTRAINT `fk_det_res_item_selected_Item_Id1` FOREIGN KEY (`Item_Id`) REFERENCES `mas_item` (`Item_Id`) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
CREATE TABLE `mas_buf_off` (
`Buf_Off_Id` int(11) unsigned NOT NULL AUTO_INCREMENT,
`Buf_Type_Id` int(11) unsigned NOT NULL,
`From_Time` time NOT NULL,
`To_Time` time NOT NULL,
`Res_Id` int(11) unsigned NOT NULL,
PRIMARY KEY (`Buf_Off_Id`),
KEY `fk_mas_buf_off_Buf_Type_Id1_Idx` (`Buf_Type_Id`),
KEY `fk_mas_buf_off_Res_Id1_Idx` (`Res_Id`),
CONSTRAINT `fk_mas_buf_off_Buf_Type_Id1` FOREIGN KEY (`Buf_Type_Id`) REFERENCES `mas_buf_type` (`Buf_Type_Id`) ON DELETE CASCADE,
CONSTRAINT `fk_mas_buf_off_Res_Id1_Id1` FOREIGN KEY (`Res_Id`) REFERENCES `mas_restaurant` (`Res_Id`) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
CREATE TABLE `mas_restaurant` (
`Res_Id` int(11) unsigned NOT NULL AUTO_INCREMENT,
`Res_Name` varchar(100) NOT NULL,
`Res_Featured` tinyint(4) NOT NULL DEFAULT '0',
PRIMARY KEY (`Res_Id`)
) ENGINE=InnoDB AUTO_INCREMENT=5 DEFAULT CHARSET=utf8;
I Just ran below script and it worked on my system.
set foreign_key_checks=0;
CREATE TABLE `det_res_item_selected` (
`Line_Selected_Item_Id` int(11) unsigned NOT NULL AUTO_INCREMENT,
`Item_Id` int(11) unsigned NOT NULL,
`Item_Selected` tinyint(4) DEFAULT '0',
`Buf_Off_Id` int(11) unsigned DEFAULT NULL,
`Item_Image` varchar(50) NOT NULL,
PRIMARY KEY (`Line_Selected_Item_Id`),
KEY `fk_det_res_item_selected_Item1_Idx` (`Item_Id`),
KEY `fk_det_res_item_selected_Buf_Off_Id1_Idx` (`Buf_Off_Id`),
CONSTRAINT `fk_det_res_item_selected_Buf_Off_Id1` FOREIGN KEY (`Buf_Off_Id`) REFERENCES `mas_buf_off` (`Buf_Off_Id`) ON DELETE CASCADE,
CONSTRAINT `fk_det_res_item_selected_Item_Id1` FOREIGN KEY (`Item_Id`) REFERENCES `mas_item` (`Item_Id`) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
CREATE TABLE `mas_buf_off` (
`Buf_Off_Id` int(11) unsigned NOT NULL AUTO_INCREMENT,
`Buf_Type_Id` int(11) unsigned NOT NULL,
`From_Time` time NOT NULL,
`To_Time` time NOT NULL,
`Res_Id` int(11) unsigned NOT NULL,
PRIMARY KEY (`Buf_Off_Id`),
KEY `fk_mas_buf_off_Buf_Type_Id1_Idx` (`Buf_Type_Id`),
KEY `fk_mas_buf_off_Res_Id1_Idx` (`Res_Id`),
CONSTRAINT `fk_mas_buf_off_Buf_Type_Id1` FOREIGN KEY (`Buf_Type_Id`) REFERENCES `mas_buf_type` (`Buf_Type_Id`) ON DELETE CASCADE,
CONSTRAINT `fk_mas_buf_off_Res_Id1_Id1` FOREIGN KEY (`Res_Id`) REFERENCES `mas_restaurant` (`Res_Id`) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
CREATE TABLE `mas_restaurant` (
`Res_Id` int(11) unsigned NOT NULL AUTO_INCREMENT,
`Res_Name` varchar(100) NOT NULL,
`Res_Featured` tinyint(4) NOT NULL DEFAULT '0',
PRIMARY KEY (`Res_Id`)
) ENGINE=InnoDB AUTO_INCREMENT=5 DEFAULT CHARSET=utf8;
set foreign_key_checks=1;

Foreign Key Constraint Fails - Probably misunderstanding something about my relationships

I'm having a little trouble with some MySQL relationships. I think I'm missing something obvious in my structure. Here's my SQL:
DROP TABLE IF EXISTS `parentlist_comments`;
CREATE TABLE `parentlist_comments` (
`id` char(36) NOT NULL,
`parentlist_id` char(36) NOT NULL,
`user_id` char(36) NOT NULL,
`comment` char(50) NOT NULL,
`accepted` tinyint(1) NOT NULL DEFAULT '0',
`submitted` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (`id`),
KEY `fk_parentlist_comments_parentlist` (`parentlist_id`),
KEY `fk_parentlist_comment_user` (`user_id`),
CONSTRAINT `fk_parentlist_comments_parentlist` FOREIGN KEY (`parentlist_id`) REFERENCES `parentlists` (`id`) ON DELETE CASCADE ON UPDATE CASCADE,
CONSTRAINT `fk_parentlist_comment_user` FOREIGN KEY (`user_id`) REFERENCES `users` (`id`) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
DROP TABLE IF EXISTS `parentlist_submissions`;
CREATE TABLE `parentlist_submissions` (
`id` char(36) NOT NULL,
`parentlist_id` char(36) NOT NULL,
`type_id` char(36) NOT NULL,
`name` char(25) NOT NULL,
`user_id` char(36) NOT NULL,
`accepted` tinyint(1) NOT NULL DEFAULT '0',
`submitted` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
`votes` int(3) NOT NULL DEFAULT '0',
PRIMARY KEY (`id`),
KEY `fk_parentlist_submissions_user` (`user_id`),
KEY `fk_parentlist_submissions_list` (`parentlist_id`),
KEY `fk_parentlist_submissions_type` (`type_id`),
CONSTRAINT `fk_parentlist_submissions_list` FOREIGN KEY (`parentlist_id`) REFERENCES `parentlists` (`id`) ON DELETE CASCADE ON UPDATE CASCADE,
CONSTRAINT `fk_parentlist_submissions_type` FOREIGN KEY (`type_id`) REFERENCES `types` (`id`) ON DELETE CASCADE ON UPDATE CASCADE,
CONSTRAINT `fk_parentlist_submissions_user` FOREIGN KEY (`user_id`) REFERENCES `users` (`id`) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
DROP TABLE IF EXISTS `parentlists`;
CREATE TABLE `parentlists` (
`id` char(36) NOT NULL,
`name` char(20) NOT NULL,
`description` char(50) NOT NULL,
`user_id` char(36) NOT NULL,
`max_comments` int(3) NOT NULL DEFAULT '0',
`max_submissions` int(3) NOT NULL DEFAULT '10',
`max_votes` int(3) NOT NULL DEFAULT '0',
PRIMARY KEY (`id`),
KEY `fk_list_user` (`user_id`),
CONSTRAINT `fk_list_user` FOREIGN KEY (`user_id`) REFERENCES `users` (`id`) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8 ROW_FORMAT=FIXED;
DROP TABLE IF EXISTS `submissions`;
CREATE TABLE `submissions` (
`id` char(36) NOT NULL,
`type_id` char(36) NOT NULL,
`name` char(30) NOT NULL,
`description` char(50) NOT NULL,
`embed` char(200) DEFAULT NULL,
`user_id` char(36) NOT NULL,
`accepted` tinyint(1) NOT NULL DEFAULT '0',
`submitted` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
`votes` int(5) NOT NULL DEFAULT '0',
PRIMARY KEY (`id`),
KEY `fk_submission_user` (`user_id`),
KEY `fk_submission_type` (`type_id`),
CONSTRAINT `fk_submission_type` FOREIGN KEY (`type_id`) REFERENCES `types` (`id`) ON DELETE CASCADE ON UPDATE CASCADE,
CONSTRAINT `fk_submission_user` FOREIGN KEY (`user_id`) REFERENCES `users` (`id`) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
DROP TABLE IF EXISTS `types`;
CREATE TABLE `types` (
`id` char(36) NOT NULL,
`name` char(20) NOT NULL,
`description` char(20) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
DROP TABLE IF EXISTS `users`;
CREATE TABLE `users` (
`id` char(36) NOT NULL,
`name` char(20) NOT NULL,
`password` char(20) NOT NULL,
`email` char(50) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
I created an column called submission_id in parentlist_submissions. I'm trying to create a foreign key relationship between parentlist_submissions.submission_id and submissions.id, when I attempt to do this I get the error: Foriegn key constraint fails. For whatever reason my query browser won't let me copy this.
Any help here is greatly appreciated!
That error is usually caused by the tables already being populated with data that violate the constraint. (Note that nulls may be a problem if you've just added the column.)
I'm guessing, because I don't see that you've posted the statement where you create the submission_index column or where you create the foreign key constraint.
You seem to be missing the "parentlist_submissions.submission_id" column.