Foreign Key Constraint Fails - Probably misunderstanding something about my relationships - mysql

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.

Related

mySQL: Cannot add foreign key constraint when creation a table

I want to create this table
CREATE TABLE `t_plan` (
`id` int(11) NOT NULL,
`name` varchar(255) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
CREATE TABLE `t_plan_x_currency` (
`plan_id` bigint(20) NOT NULL ,
`currency_symbol` varchar(20) NOT NULL DEFAULT 'EUR',
`price` decimal(12,6) NOT NULL DEFAULT '0',
PRIMARY KEY (`plan_id`,`currency_symbol`),
CONSTRAINT `t_plan_x_currency_ibfk_1` FOREIGN KEY (`plan_id`) REFERENCES `t_plan` (`id`) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=latin1
but I got this error:
Cannot add foreign key constraint
The columns involved in a foreign key relationship have to be of the same type. You have different types, int(11) for t_plan.id and bigint(20) for t_plan_x_currency.plan_id.
Make t_plan.id also a bigint(20).
CREATE TABLE `t_plan` (
`id` bigint(20) NOT NULL,
`name` varchar(255) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
CREATE TABLE `t_plan_x_currency` (
`plan_id` bigint(20) NOT NULL ,
`currency_symbol` varchar(20) NOT NULL DEFAULT 'EUR',
`price` decimal(12,6) NOT NULL DEFAULT '0',
PRIMARY KEY (`plan_id`,`currency_symbol`),
CONSTRAINT `t_plan_x_currency_ibfk_1` FOREIGN KEY (`plan_id`) REFERENCES `t_plan` (`id`) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=latin1
Or make t_plan_x_currency.plan_id an int(11), what ever is more desirable for you.

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.

Foreign key constraint fails when I want delete table

When I am trying
drop tables users;
It shows error: Cannot delete or update a parent row: a foreign key constraint fails
Here are my tables:
CREATE TABLE `events` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`title` varchar(128) NOT NULL,
`users_id` int(11) NOT NULL,
`capacity` int(64) NOT NULL,
`event_date` datetime NOT NULL,
`text` text CHARACTER SET utf8 COLLATE utf8_czech_ci,
`last_edit` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
PRIMARY KEY (`id`),
KEY `users_id` (`users_id`),
CONSTRAINT `events_ibfk_5` FOREIGN KEY (`users_id`) REFERENCES `users` (`id`) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
CREATE TABLE `users` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`email` varchar(100) NOT NULL,
`password` varchar(100) NOT NULL,
`role` enum('admin','member','guest','registered') NOT NULL DEFAULT 'registered',
PRIMARY KEY (`id`),
UNIQUE KEY `email` (`email`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
CREATE TABLE `users_data` (
`id` int(120) NOT NULL AUTO_INCREMENT,
`users_id` int(11) DEFAULT NULL,
`name` varchar(50) NOT NULL,
`username` varchar(50) NOT NULL,
`phone_number` varchar(25) NOT NULL,
PRIMARY KEY (`id`),
KEY `users_id` (`users_id`),
CONSTRAINT `users_data_ibfk_5` FOREIGN KEY (`users_id`) REFERENCES `users` (`id`) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
CREATE TABLE `user_matches` (
`user_id` int(120) NOT NULL,
`match_id` int(11) NOT NULL,
KEY `user_id` (`user_id`),
KEY `match_id` (`match_id`),
CONSTRAINT `user_matches_ibfk_4` FOREIGN KEY (`user_id`) REFERENCES `users_data` (`id`) ON DELETE CASCADE ON UPDATE CASCADE,
CONSTRAINT `user_matches_ibfk_5` FOREIGN KEY (`match_id`) REFERENCES `users` (`id`) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
I think I did wrong foreign key but I am not sure how to change it correctly. Thank you for your any advice.
When you submit drop table users MySQL checks for referential integrity constraints violation when the deletion is about to be committed. This check fails because you have defined the following constraint of events:
CONSTRAINTevents_ibfk_5FOREIGN KEY (users_id) REFERENCESusers(id) ON DELETE CASCADE ON UPDATE CASCADE
and at the same time on the events table the users_id field cannot be NULL. Therefore, upon dropping the table, the users_id is set to NULL which fails during commit. One possible way to work around this problem is to remove the NOT NULL constraint on the users_id field on the events table.

(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;

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

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.