mySQL: Cannot add foreign key constraint when creation a table - mysql

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.

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.

ERROR: ERROR 1215 (HY000): Cannot add foreign key constraint

I was helping a friend migrate his WordPress to AWS. The whole WordPress was exported by him using WPClone and I'm going to import it to RDS (Aurora). I'm not a pro DB guy and I only knew basic MySQL commands. I was trying to restore a database but during the restoration I encountered ERROR: ERROR 1215 (HY000): Cannot add foreign key constraint.
I executed show engine innodb status and checked the LATEST FOREIGN KEY ERROR and this is what I got but I have no idea how to resolve this.
------------------------
LATEST FOREIGN KEY ERROR
------------------------
2017-03-10 11:22:06 2b87437cb700 Error in foreign key constraint of table clientdb/wp_supsystic_ss_project_networks:
FOREIGN KEY (`project_id`) REFERENCES `wp_supsystic_ss_projects` (`id`) ON DELETE CASCADE
) ENGINE=InnoDB AUTO_INCREMENT=5 DEFAULT CHARSET=utf8:
Cannot resolve table name close to:
(`id`) ON DELETE CASCADE
) ENGINE=InnoDB AUTO_INCREMENT=5 DEFAULT CHARSET=utf8
Help would be greatly appreciated!
Edit: Here's the create table for wp_supsystic_ss_networks, wp_supsystic_ss_project_networks and wp_supsystic_ss_projects:
CREATE TABLE `wp_supsystic_ss_networks` (
`id` int(11) unsigned NOT NULL AUTO_INCREMENT,
`name` varchar(255) NOT NULL,
`url` varchar(255) NOT NULL,
`class` varchar(255) NOT NULL,
`brand_primary` varchar(7) NOT NULL DEFAULT '#000000',
`brand_secondary` varchar(7) NOT NULL DEFAULT '#ffffff',
`total_shares` int(11) unsigned DEFAULT '0',
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=20 DEFAULT CHARSET=utf8;
CREATE TABLE `wp_supsystic_ss_project_networks` (
`id` int(11) unsigned NOT NULL AUTO_INCREMENT,
`project_id` int(11) unsigned DEFAULT NULL,
`network_id` int(11) unsigned DEFAULT NULL,
`position` int(11) unsigned DEFAULT '0',
`title` varchar(255) DEFAULT NULL,
`text` varchar(255) DEFAULT NULL,
`tooltip` varchar(255) DEFAULT NULL,
`text_format` varchar(255) DEFAULT NULL,
`use_short_url` bit(1) DEFAULT NULL,
`icon_image` int(11) DEFAULT NULL,
PRIMARY KEY (`id`),
KEY `FK__wp_supsystic_ss_projects` (`project_id`),
KEY `FK__wp_supsystic_ss_networks` (`network_id`),
CONSTRAINT `FK__wp_supsystic_ss_networks` FOREIGN KEY (`network_id`) REFERENCES `wp_supsystic_ss_networks` (`id`),
CONSTRAINT `FK__wp_supsystic_ss_projects` FOREIGN KEY (`project_id`) REFERENCES `wp_supsystic_ss_projects` (`id`) ON DELETE CASCADE
) ENGINE=InnoDB AUTO_INCREMENT=5 DEFAULT CHARSET=utf8;
CREATE TABLE `wp_supsystic_ss_projects` (
`id` int(11) unsigned NOT NULL AUTO_INCREMENT,
`title` varchar(255) NOT NULL,
`created_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
`settings` text NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8;
Two problems:
Create tables in this order: child tables, parent table (with foreign keys). Replace CREATE TABLE statements:
wp_supsystic_ss_projects
wp_supsystic_ss_networks
wp_supsystic_ss_project_networks.
Child table wp_supsystic_ss_networks does not exist in the script.

Export localhost Sql and import it on Server gives me errno 150

I suggest something is wrong with the foreign keys, but i can't find a hint.
error while importing the sql:
ALTER TABLE `claim`
ADD CONSTRAINT `FK_66A8F1231B7B246A` FOREIGN KEY (`purchaseOrder_id`) REFERENCES `PurchaseOrder` (`id`) ON DELETE CASCADE,
ADD CONSTRAINT `FK_66A8F1237294869C` FOREIGN KEY (`article_id`) REFERENCES `Article` (`id`);
#1005 - Can't create table 'databasename.#sql-5c7_568c0' (errno: 150)
I also checked the conditions of this answer MySQL Creating tables with Foreign Keys giving errno: 150
But i couldn't find the mistake.
Any guesses?
Tables:
CREATE TABLE `Article` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
`number` int(11) NOT NULL,
`currency` tinyint(1) NOT NULL,
`price` double NOT NULL,
`tolerance` int(11) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
-- --------------------------------------------------------
CREATE TABLE `claim` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`comment` longtext COLLATE utf8_unicode_ci NOT NULL,
`purchaseOrder_id` int(11) DEFAULT NULL,
`visible` tinyint(1) NOT NULL,
`article_id` int(11) DEFAULT NULL,
PRIMARY KEY (`id`),
KEY `IDX_66A8F1231B7B246A` (`purchaseOrder_id`),
KEY `IDX_66A8F1237294869C` (`article_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
CREATE TABLE `purchaseorder` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`date` datetime NOT NULL,
`state` int(11) NOT NULL,
`supplier_id` int(11) DEFAULT NULL,
`delivery_id` int(11) DEFAULT NULL,
PRIMARY KEY (`id`),
KEY `IDX_AA004F112ADD6D8C` (`supplier_id`),
KEY `IDX_AA004F1112136921` (`delivery_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
Your table name is purchaseorder
--------------------------^-----------^
ALTER TABLE `claim`
ADD CONSTRAINT `FK_66A8F1231B7B246A` FOREIGN KEY (`purchaseOrder_id`) REFERENCES `purchaseorder` (`id`) ON DELETE CASCADE,
ADD CONSTRAINT `FK_66A8F1237294869C` FOREIGN KEY (`article_id`) REFERENCES `Article` (`id`);

#1005 - Can't create table 'table_name' (errno: 150)

here is my tables:
DROP TABLE IF EXISTS `tbl_comments`;
CREATE TABLE IF NOT EXISTS `tbl_comments` (
`id` int(11) NOT NULL auto_increment,
`topic_id` int(11) NOT NULL,
`user_id` int(11) NOT NULL,
`text` text NOT NULL,
`create_dt` datetime NOT NULL,
`update_dt` timestamp NULL default CURRENT_TIMESTAMP,
PRIMARY KEY (`id`),
KEY `topic_id_2` (`topic_id`),
KEY `user_id` (`user_id`),
CONSTRAINT `tbl_comments_ibfk_2` FOREIGN KEY (`user_id`) REFERENCES `tbl_users` (`id`),
CONSTRAINT `tbl_comments_ibfk_1` FOREIGN KEY (`topic_id`) REFERENCES `tbl_topics` (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=9 DEFAULT CHARSET=utf8;
DROP TABLE IF EXISTS `tbl_users`;
CREATE TABLE IF NOT EXISTS `tbl_users` (
`id` int(11) NOT NULL auto_increment,
`username` varchar(20) NOT NULL default '',
`password` varchar(128) NOT NULL default '',
`email` varchar(128) NOT NULL default '',
`activkey` varchar(128) NOT NULL default '',
`superuser` int(1) NOT NULL default '0',
`status` int(1) NOT NULL default '0',
`create_at` timestamp NOT NULL default CURRENT_TIMESTAMP,
`lastvisit_at` timestamp NOT NULL default '0000-00-00 00:00:00',
PRIMARY KEY (`id`),
UNIQUE KEY `user_username` (`username`),
UNIQUE KEY `user_email` (`email`)
) ENGINE=InnoDB AUTO_INCREMENT=14 DEFAULT CHARSET=utf8;
DROP TABLE IF EXISTS `tbl_topics`;
CREATE TABLE IF NOT EXISTS `tbl_topics` (
`id` int(11) NOT NULL auto_increment,
`group_id` int(11) NOT NULL,
`user_id` int(11) NOT NULL,
`header` varchar(255) NOT NULL,
`text` text NOT NULL,
`create_dt` datetime NOT NULL,
`update_dt` timestamp NULL default CURRENT_TIMESTAMP,
PRIMARY KEY (`id`),
KEY `user_id` (`user_id`),
KEY `group_id` (`group_id`),
CONSTRAINT `tbl_topics_ibfk_1` FOREIGN KEY (`group_id`) REFERENCES `tbl_groups` (`id`),
CONSTRAINT `tbl_topics_ibfk_2` FOREIGN KEY (`user_id`) REFERENCES `tbl_users` (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=9 DEFAULT CHARSET=utf8;
now i have the error when i try to import:
#1005 - Can't create table 'kobeco_yii.tbl_comments' (errno: 150) (Details: Percona-XtraDB, Supports transactions, row-level locking, and foreign keys )
You're trying to create tbl_comments before your other tables. tbl_comments requires tables tbl_users and tbl_topics for foreign key constraints.
Try moving the CREATE TABLE for tbl_comments to below the others.
Your first table (table_commets) has constraints to tables that doesn't exist yet. This may be an issue of you are creating them in that order.
Create table_usr and table_topics first.

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.