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

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`);

Related

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.

Cannot add foreign key constraint

I have created a database "webportal". and this is my "user" table script
DROP TABLE IF EXISTS `user`;
CREATE TABLE `user` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`username` varchar(255) DEFAULT NULL,
`password` varchar(255) DEFAULT NULL,
`firstName` varchar(255) DEFAULT NULL,
`lastName` varchar(255) DEFAULT NULL,
`dateRegistered` DATE DEFAULT NULL,
`skypeID` int(11) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8;
and this one is "catalog" table script.
DROP TABLE IF EXISTS `catalog`;
create table catalog(
`id` int(11) NOT NULL AUTO_INCREMENT,
`user_id` int(11) NOT NULL,
`link` VARCHAR(100) NOT NULL,
`comment` VARCHAR(100) NOT NULL,
`inserDate` DATE DEFAULT NULL,
`content` longblob NOT NULL,
PRIMARY KEY (`id`),
CONSTRAINT `fk_catalog` FOREIGN KEY (`user_id`) REFERENCES `user` (`id`) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
and when I try to execute the second script in the command line, I get this error...
ERROR 1215 (HY000): Cannot add foreign key constraint.
What is wrong with this code?
It seems you are using a old version of MySQL, you can add a INDEX clause to foreign key field to fix the problem:
DROP TABLE IF EXISTS `catalog`;
create table `catalog`(
`id` int(11) NOT NULL AUTO_INCREMENT,
`user_id` int(11) NOT NULL,
`link` VARCHAR(100) NOT NULL,
`comment` VARCHAR(100) NOT NULL,
`inserDate` DATE DEFAULT NULL,
`content` longblob NOT NULL,
PRIMARY KEY (`id`),
INDEX (`user_id`),
CONSTRAINT `fk_catalog` FOREIGN KEY (`user_id`) REFERENCES `user` (`id`) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

Cannot create foreign key in MySQL error: 150

I have created the following tables and have no idea why my foreign key constraint script is not working.
CREATE TABLE IF NOT EXISTS `project` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`project_id` varchar(60) NOT NULL,
`project_name` varchar(500) NOT NULL,
`cons_bal` int(11) NOT NULL,
`non_cons_bal` int(11) NOT NULL,
`budget_head` varchar(100) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=1 ;
And here is my another table:
CREATE TABLE IF NOT EXISTS `project_map` (
`id` int(11) NOT NULL,
`project_id` varchar(60) NOT NULL,
`head` int(11) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
And here is my add constraint line:
alter table project_map add constraint p_map_fk001 foreign key (`project_id`) references project(`project_id`)
Any help would be nice. Thank you.
In the referenced table, there must be an index where the referenced columns are listed as the first columns in the same order.
Refer: http://dev.mysql.com/doc/refman/5.6/en/innodb-foreign-key-constraints.html
So try:
CREATE TABLE `project` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`project_id` varchar(60) NOT NULL,
`project_name` varchar(500) NOT NULL,
`cons_bal` int(11) NOT NULL,
`non_cons_bal` int(11) NOT NULL,
`budget_head` varchar(100) NOT NULL,
PRIMARY KEY (`id`),
KEY `project_id` (`project_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8
CREATE TABLE `project_map` (
`id` int(11) NOT NULL,
`project_id` varchar(60) NOT NULL,
`head` int(11) NOT NULL,
PRIMARY KEY (`id`),
KEY `project_id` (`project_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8
ALTER TABLE `project_map` ADD CONSTRAINT
`p_map_fk001` FOREIGN KEY (`project_id`)
REFERENCES project(`project_id`);

Mysql cross-database foreign key

I am trying to create a cross database foreign key. When I run the following code on the same database
CREATE TABLE `t1` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`int_id` varchar(128) COLLATE utf8_unicode_ci NOT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `int_id` (`int_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
CREATE TABLE `t2` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`ext_id` varchar(128) COLLATE utf8_unicode_ci NOT NULL,
PRIMARY KEY (`id`),
KEY `ext_id` (`ext_id`),
CONSTRAINT `t2_ibfk_1` FOREIGN KEY (`ext_id`) REFERENCES `t1` (`int_id`) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
However, when create t1 on one databse (d1) and then run the following code on a second databse (d2), I receive the generic error: #1005 - Can't create table 'userdata.t2' (errno: 150)
CREATE TABLE `t2` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`ext_id` varchar(128) COLLATE utf8_unicode_ci NOT NULL,
PRIMARY KEY (`id`),
KEY `ext_id` (`ext_id`),
CONSTRAINT `t2_ibfk_1` FOREIGN KEY (`ext_id`) REFERENCES `d1.t1` (`int_id`) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
Any help will be appreciated. Thanks!
Your syntax is not correct, try this
CREATE TABLE `t2` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`ext_id` varchar(128) COLLATE utf8_unicode_ci NOT NULL,
PRIMARY KEY (`id`),
KEY `ext_id` (`ext_id`),
CONSTRAINT `t2_ibfk_1` FOREIGN KEY (`ext_id`) REFERENCES `d1`.`t1` (`int_id`) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;

#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.