Search code examples
mysqlwordpressamazon-web-servicesrdsamazon-aurora

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;

Solution

  • Two problems:

    1. 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.
    2. Child table wp_supsystic_ss_networks does not exist in the script.