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

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.

Related

"Foreign key constraint is incorrectly formed" Trying to make a old project work

So i'm trying to make a old project work again but when i put back my tables to the new database i'm having this issue
ERROR 1005 (HY000) at line 777: Can't create table ****.#sql-3f2_45 (errno: 150 "Foreign key constraint is incorrectly formed")
There is the table that causing problems:
CREATE TABLE `install__dashboards` (
`id` int(11) NOT NULL PRIMARY KEY,
`zone` varchar(255) CHARACTER SET utf8 DEFAULT NULL,
`protocol` varchar(255) CHARACTER SET utf8 DEFAULT NULL,
`agent` varchar(255) CHARACTER SET utf8 DEFAULT NULL,
`object` varchar(255) CHARACTER SET utf8 DEFAULT NULL,
`type` set('dashboard','visualization','search') DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
--
-- Contraintes pour la table `install__dashboards`
--
ALTER TABLE `install__dashboards`
ADD CONSTRAINT `dash_agent` FOREIGN KEY (`agent`) REFERENCES `install__agents` (`name`),
ADD CONSTRAINT `dash_proto` FOREIGN KEY (`protocol`) REFERENCES `install__ports` (`protocol`),
ADD CONSTRAINT `dash_zone` FOREIGN KEY (`zone`) REFERENCES `install__zone` (`slug`);
References tables :
CREATE TABLE `install__agents` (
`id` int(11) NOT NULL,
`OS` varchar(255) NOT NULL,
`name` varchar(255) NOT NULL,
`zone` varchar(255) NOT NULL,
`IP` text,
`isLog` tinyint(1) NOT NULL DEFAULT '0'
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
CREATE TABLE `install__ports` (
`id` int(11) NOT NULL,
`numero` int(11) NOT NULL,
`protocol` varchar(255) NOT NULL,
`isUDP` tinyint(1) NOT NULL DEFAULT '0',
`machine` varchar(255) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
CREATE TABLE `install__zone` (
`slug` varchar(255) NOT NULL,
`status` enum('notpresent','installing','installed') NOT NULL,
`options` text,
`IPrange` text,
`IPsystem` text,
`system` varchar(255) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
To be honest, the error message isn't very descriptive :-(
To get more details about the cause of the error, just check InnoDB status:
$ mysql -e"SHOW ENGINE INNODB STATUS\G" | grep -C3 "FOREIGN KEY ERROR"
SEMAPHORES
----------
------------------------
LATEST FOREIGN KEY ERROR
------------------------
2022-12-06 11:08:12 0x7f43a80ba700 Error in foreign key constraint of table `test`.`install__dashboards`:
Alter table `test`.`install__dashboards` with foreign key `dash_proto` constraint failed. There is no index in the referenced table where the referenced columns appear as the first columns.------------
So the problem/reason is There is no index in the referenced table where the referenced columns appear as the first columns
You need to create indexes on the referencing column:
CREATE INDEX `ix_install__agents_name` ON `install__agents` (`name`);
CREATE INDEX `ix_install__ports_protocol` ON `install__ports` (`protocol`);
CREATE INDEX `ix_install__zone_slug` ON `install__zone` (`slug`);
but your schema does not look right. It's better to have the ID of the entity.
Something like this:
CREATE TABLE `install__agents` (
`id` int(11) NOT NULL PRIMARY KEY,
`OS` varchar(255) NOT NULL,
`name` varchar(255) NOT NULL,
`zone` varchar(255) NOT NULL,
`IP` text,
`isLog` tinyint(1) NOT NULL DEFAULT '0'
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
CREATE TABLE `install__ports` (
`id` int(11) NOT NULL PRIMARY KEY,
`numero` int(11) NOT NULL,
`protocol` varchar(255) NOT NULL,
`isUDP` tinyint(1) NOT NULL DEFAULT '0',
`machine` varchar(255) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
CREATE TABLE `install__zone` (
`id` int(11) NOT NULL PRIMARY KEY,
`slug` varchar(255) NOT NULL,
`status` enum('notpresent','installing','installed') NOT NULL,
`options` text,
`IPrange` text,
`IPsystem` text,
`system` varchar(255) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
CREATE TABLE `install__dashboards` (
`id` int(11) NOT NULL PRIMARY KEY,
`zone_id` int(11) DEFAULT NULL,
`protocol_id` int(11) DEFAULT NULL,
`agent_id` int(11)DEFAULT NULL,
`object` varchar(255) CHARACTER SET utf8 DEFAULT NULL,
`type` set('dashboard','visualization','search') DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
ALTER TABLE `install__dashboards`
ADD CONSTRAINT `dash_agent` FOREIGN KEY (`agent_id`) REFERENCES `install__agents` (`id`),
ADD CONSTRAINT `dash_proto` FOREIGN KEY (`protocol_id`) REFERENCES `install__ports` (`id`),
ADD CONSTRAINT `dash_zone` FOREIGN KEY (`zone_id`) REFERENCES `install__zone` (`id`);

Foreign key incorrectly formed error in MySQL in Manjaro Linux

"user table" is created successfully. Every columns and types is the same as "users table." But when I create an association table named "user_role," "user table" cannot be called. I only get this error in using MySQL with MariaDB. Installing mysql in windows and using like that is fine. Without changing that table name, is there anything I can do to make it right?
Two tables are dumped because I forget where did I put my original code. But I'll be the same.
CREATE TABLE `user` (
`id` bigint(20) NOT NULL AUTO_INCREMENT,
`email` varchar(255) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`name` varchar(255) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`course_id` bigint(20) DEFAULT NULL,
`password` varchar(100) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
PRIMARY KEY (`id`),
KEY `FKj8ce5cjkm11igsffixdxexrr9` (`course_id`)
) ENGINE=MyISAM AUTO_INCREMENT=5 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
CREATE TABLE `users` (
`id` bigint(20) NOT NULL AUTO_INCREMENT,
`name` varchar(50) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`email` varchar(50) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`course_id` bigint(20) DEFAULT NULL,
`password` varchar(100) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
PRIMARY KEY (`id`),
KEY `course_id` (`course_id`),
CONSTRAINT `users_ibfk_1` FOREIGN KEY (`course_id`) REFERENCES `course` (`id`) ON DELETE SET NULL) ENGINE=InnoDB AUTO_INCREMENT=9 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
CREATE TABLE `role` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`role_name` varchar(50) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
create table `user_role`(
`id` int auto_increment not null,
`user_id_fk` bigint,
`role_id_fk` int,
primary key (`id`),
foreign key (`user_id_fk`) references `user`(`id`),
foreign key (`role_id_fk`) references `role`(`id`)
);
--> references user(id) is not working but references users(id) is working. Is there any way to make it work? Or just change the name?
Suggest you don't have one table name being the plural of another (user vs users); it can lead to typos.
Don't use MyISAM for anything. (with rare exceptions)
If that is a many-to-many mapping table follow the tips in http://mysql.rjweb.org/doc.php/index_cookbook_mysql#many_to_many_mapping_table
The foreign key is across tables defined with different engines(InnoDB and MyISAM):
create table `user_role`(
`id` int auto_increment not null,
`user_id_fk` bigint(20), -- matching data types
`role_id_fk` int(11) -- matching data types
,primary key (`id`)
,foreign key (`role_id_fk`) references `role`(`id`)
,foreign key (`user_id_fk`) references `user`(`id`)
);
db<>fiddle demo
Related: Why doesn't MySQL's MyISAM engine support Foreign keys?

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.

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;

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