not able to apply foreign key constraint in mysql - mysql

I can run the below query and there are no nulls in the user_id column in the result -
select c.*,r.user_id from chat_group_users c left join roleuser r
on c.user_id = r.user_id;
But I cannot apply the foreign key constraint and mysql just says
Error Code: 1215. Cannot add foreign key constraint
The command i wrote -
ALTER TABLE chat_group_users
ADD CONSTRAINT fk_roleuser FOREIGN KEY (user_id) REFERENCES roleuser (user_id)
ON DELETE cascade ON UPDATE cascade;
The table structures -
CREATE TABLE `roleuser` (
`User_ID` int(10) unsigned NOT NULL AUTO_INCREMENT,
`UserNM` varchar(50) NOT NULL,
`UserPS` varchar(150) NOT NULL,
`r_username` varchar(50) DEFAULT NULL,
`UGroup` varchar(5) NOT NULL DEFAULT 'Usar',
`FirstName` varchar(45) NOT NULL,
`LastName` varchar(45) NOT NULL,
PRIMARY KEY (`User_ID`),
UNIQUE KEY `Index_2` (`UserNM`),
) ENGINE=InnoDB AUTO_INCREMENT=50 DEFAULT CHARSET=latin1;
CREATE TABLE `chat_group_users` (
`auto_id` int(11) NOT NULL AUTO_INCREMENT,
`group_id` int(11) DEFAULT NULL,
`user_id` int(10) DEFAULT NULL,
`is_admin` varchar(5) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
PRIMARY KEY (`auto_id`),
UNIQUE KEY `unique_user_group` (`user_id`,`group_id`),
KEY `fc_group_id` (`group_id`),
CONSTRAINT `fc_group_id` FOREIGN KEY (`group_id`)
REFERENCES `chat_group`(`auto_id`)
ON DELETE CASCADE ON UPDATE CASCADE
)ENGINE=InnoDB AUTO_INCREMENT=21 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

missed the unsigned on the chat_group_users -
`User_ID` int(10) unsigned NOT NULL AUTO_INCREMENT, ...

Related

MySQL Missing comma before start of a new alter operation. (near "ADD" at position 255)

I have a problem
my code
_________
-- phpMyAdmin SQL Dump
-- version 4.7.4
-- https://www.phpmyadmin.net/
--
-- --------------------------------------------------------
CREATE TABLE IF NOT EXISTS `country` (
`id` smallint(5) unsigned NOT NULL AUTO_INCREMENT,
`alpha2` varchar(2) NOT NULL,
`alpha3` varchar(3) NOT NULL,
`name` varchar(45) NOT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `alpha2` (`alpha2`),
UNIQUE KEY `alpha3` (`alpha3`)
) ENGINE=InnoDB;
--
-- `user` table structure
--
CREATE TABLE IF NOT EXISTS `user` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`firstname` varchar(50) NOT NULL,
`lastname` varchar(50) NOT NULL,
`email` varchar(50) NOT NULL,
`login` varchar(50) NOT NULL,
`password` varchar(50) NOT NULL,
`avatar` varchar(50) NOT NULL,
CONSTRAINT `user_pk` PRIMARY KEY (`id`),
CONSTRAINT `user_idx_1` UNIQUE INDEX (`login`)
) ENGINE=InnoDB;
CREATE TABLE IF NOT EXISTS `user_address` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`user_id` int(11) NOT NULL,
`address_line_1` varchar(40) NOT NULL,
`address_line_2` varchar(40) NOT NULL,
`address_line_3` varchar(40) NOT NULL,
`address_line_4` varchar(40) NOT NULL,
`address_line_5` varchar(40) NOT NULL,
`postal_code` varchar(40) NOT NULL,
`city_name` varchar(40) NOT NULL,
`country_code` varchar(3) NOT NULL,
CONSTRAINT `user__address_pk` PRIMARY KEY (`id`),
INDEX `user_address_idx_1` (`user_id`),
INDEX `user_address_idx_2` (`country_code`),
CONSTRAINT `user_address_ibfk_1` FOREIGN KEY (`user_id`) REFERENCES `user` (`id`),
CONSTRAINT `user_address_ibfk_2` FOREIGN KEY (`country_code`) REFERENCES `country` (`alpha3`)
) ENGINE=InnoDB;
ALTER TABLE `user`
ADD COLUMN IF NOT EXISTS `shipping_address` int(11) NULL,
ADD COLUMN IF NOT EXISTS `billing_address` int(11) NULL,
ADD CONSTRAINT IF NOT EXISTS `user_ibfk_2` FOREIGN KEY (`shipping_address`) REFERENCES `user_address` (`id`)
ADD CONSTRAINT IF NOT EXISTS `user_ibfk_3` FOREIGN KEY (`billing_address`) REFERENCES `user_address` (`id`);
CREATE INDEX IF NOT EXISTS `user_idx_2` ON `user`(`shipping_address`);
CREATE INDEX IF NOT EXISTS `user_idx_3` ON `user`(`billing_address`);
CREATE TABLE IF NOT EXISTS `product_types` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`type` varchar(50) NOT NULL,
`description` varchar(4000) DEFAULT NULL,
CONSTRAINT `product_types_pk` PRIMARY KEY (`id`),
CONSTRAINT `product_types_idx_1` UNIQUE INDEX (`type`)
) ENGINE=InnoDB;
CREATE TABLE IF NOT EXISTS `products` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`code` varchar(15) NOT NULL,
`name` varchar(70) NOT NULL,
`type` int(11) NOT NULL,
`scale` varchar(10) NOT NULL,
`vendor` varchar(50) NOT NULL,
`description` text NOT NULL,
`stock_level` smallint(6) NOT NULL,
`cost` decimal(10,2) NOT NULL,
`price` decimal(10,2) NOT NULL,
CONSTRAINT `product_pk` PRIMARY KEY (`id`),
INDEX `product_idx_2` (`type`),
CONSTRAINT `product_idx_1` UNIQUE INDEX (`code`),
CONSTRAINT `products_ibfk_1` FOREIGN KEY (`type`) REFERENCES `product_types` (`id`)
) ENGINE=InnoDB;
ALTER TABLE `products` ADD FULLTEXT(`description`);
CREATE TABLE IF NOT EXISTS `orders` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`order_date` timestamp NOT NULL DEFAULT current_timestamp(),
`shipped_date` timestamp DEFAULT NULL,
`status` varchar(15) NOT NULL,
`customer_id` int(11) NOT NULL,
CONSTRAINT `order_pk` PRIMARY KEY (`id`),
INDEX `order_idx_1` (`customer_id`),
CONSTRAINT `orders_ibfk_1` FOREIGN KEY (`customer_id`) REFERENCES `user` (`id`)
) ENGINE=InnoDB;
CREATE TABLE IF NOT EXISTS `order_lines` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`order_id` int(11) NOT NULL,
`product_id` int(11) NOT NULL,
`quantity` smallint(6) NOT NULL,
`cost` decimal(10,2) NOT NULL,
`price` decimal(10,2) NOT NULL,
CONSTRAINT `order_lines_pk` PRIMARY KEY (`id`),
INDEX `order_lines_idx_1` (`order_id`),
CONSTRAINT `order_lines_idx_2` UNIQUE INDEX (`product_id`),
CONSTRAINT `order_lines_ibfk_1` FOREIGN KEY (`order_id`) REFERENCES `orders` (`id`),
CONSTRAINT `order_lines_ibfk_2` FOREIGN KEY (`product_id`) REFERENCES `products` (`id`)
) ENGINE=InnoDB;
CREATE TABLE IF NOT EXISTS `order_address` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`order_id` int(11) NOT NULL,
`type` ENUM ('SHIPPING', 'BILLING'),
`address_line_1` varchar(40) NOT NULL,
`address_line_2` varchar(40) NOT NULL,
`address_line_3` varchar(40) NOT NULL,
`address_line_4` varchar(40) NOT NULL,
`address_line_5` varchar(40) NOT NULL,
`postal_code` varchar(40) NOT NULL,
`city_name` varchar(40) NOT NULL,
`country_code` varchar(3) NOT NULL,
CONSTRAINT `order_address_pk` PRIMARY KEY (`id`),
INDEX `order_address_idx_1` (`user_id`),
INDEX `order_address_idx_2` (`country_code`),
CONSTRAINT `order_address_ibfk_1` FOREIGN KEY (`order_id`) REFERENCES `orders` (`id`),
CONSTRAINT `order_address_ibfk_2` FOREIGN KEY (`country_code`) REFERENCES `country` (`alpha3`)
) ENGINE=InnoDB;
_________________________
ERROR
Static analysis:
1 errors were found during analysis.
Missing comma before start of a new alter operation. (near "ADD" at position 255)
SQL query:
ALTER TABLE `user`
ADD COLUMN IF NOT EXISTS `shipping_address` int(11) NULL,
ADD COLUMN IF NOT EXISTS `billing_address` int(11) NULL,
ADD CONSTRAINT IF NOT EXISTS `user_ibfk_2` FOREIGN KEY (`shipping_address`) REFERENCES `user_address` (`id`)
ADD CONSTRAINT IF NOT EXISTS `user_ibfk_3` FOREIGN KEY (`billing_address`) REFERENCES `user_address` (`id`)
MySQL said: Documentation
#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'FOREIGN KEY (`shipping_address`) REFERENCES `user_address` (`id`) ADD CONS...' at line 4
You don't have a comma separating your last two lines of SQL.
Please use this:
ALTER TABLE `user`
ADD COLUMN IF NOT EXISTS `shipping_address` int(11) NULL,
ADD COLUMN IF NOT EXISTS `billing_address` int(11) NULL,
ADD CONSTRAINT IF NOT EXISTS `user_ibfk_2` FOREIGN KEY (`shipping_address`) REFERENCES `user_address` (`id`),
ADD CONSTRAINT IF NOT EXISTS `user_ibfk_3` FOREIGN KEY (`billing_address`) REFERENCES `user_address` (`id`)
SQL will separate the interests of different statements with the Comma, This works similar to a Javascript Semi colon

mysql ignore index for order by on join

CREATE TABLE `call_session` (
`id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
`contact_id` bigint(20) unsigned NOT NULL,
`campaign_id` bigint(20) unsigned DEFAULT NULL,
`user_id` bigint(20) unsigned DEFAULT NULL,
`start_time` datetime DEFAULT NULL,
`end_time` datetime DEFAULT NULL,
`type` varchar(16) DEFAULT NULL,
`status` varchar(16) DEFAULT NULL,
`continue_reason_id` bigint(20) unsigned DEFAULT NULL,
`continue_by` bigint(20) unsigned DEFAULT NULL,
`end_reason_id` bigint(20) unsigned DEFAULT NULL,
`comment` varchar(255) DEFAULT '',
`call_count` int(11) NOT NULL DEFAULT '0',
`answered_call_count` int(11) NOT NULL DEFAULT '0',
PRIMARY KEY (`id`),
KEY `ix_call_session_contact_id` (`contact_id`),
KEY `ix_call_session_user_id` (`user_id`),
KEY `ix_call_session_campaign_id` (`campaign_id`),
KEY `call_session_continue_by_foreign` (`continue_by`),
KEY `call_session_end_reason_id_foreign` (`end_reason_id`),
KEY `ix_call_session_end_time` (`end_time`),
KEY `ix_call_session_start_time` (`start_time`),
KEY `ix_call_session_continue_reason_id` (`continue_reason_id`),
CONSTRAINT `call_session_campaign_id_foreign` FOREIGN KEY (`campaign_id`) REFERENCES `campaign` (`id`) ON DELETE SET NULL ON UPDATE CASCADE,
CONSTRAINT `call_session_continue_by_foreign` FOREIGN KEY (`continue_by`) REFERENCES `user` (`id`) ON DELETE SET NULL ON UPDATE CASCADE,
CONSTRAINT `call_session_continue_reason_id_foreign` FOREIGN KEY (`continue_reason_id`) REFERENCES `continue_reason` (`id`) ON DELETE NO ACTION ON UPDATE NO ACTION,
CONSTRAINT `call_session_end_reason_id_foreign` FOREIGN KEY (`end_reason_id`) REFERENCES `end_reason` (`id`) ON DELETE SET NULL ON UPDATE CASCADE,
CONSTRAINT `call_session_user_id_foreign` FOREIGN KEY (`user_id`) REFERENCES `user` (`id`) ON DELETE SET NULL ON UPDATE CASCADE
)
CREATE TABLE `continue_reason` (
`id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
`name` varchar(255) NOT NULL,
`project_id` bigint(20) unsigned DEFAULT NULL,
`sort` int(11) NOT NULL DEFAULT '0',
PRIMARY KEY (`id`),
KEY `ix_continue_reason_project_id` (`project_id`),
CONSTRAINT `continue_reason_project_id_foreign` FOREIGN KEY (`project_id`) REFERENCES `project` (`id`) ON DELETE CASCADE ON UPDATE CASCADE
)
The call_session table contain more than 500,000 rows.
The continue_reason table contain only one row.
When I join the continue_reason table for the name column, mysql ignore the start_date index so the query takes between 1.8 to 3 seconds.
I can't figure out why.
The problematic query is:
SELECT `call_session`.`id`,
`continue_reason`.`name`
FROM `call_session`
LEFT JOIN `continue_reason` ON `continue_reason`.`id` = `call_session`.`continue_reason_id`
ORDER BY `call_session`.`start_time` DESC LIMIT 1;
Explain result:
Same query without join works great:
SELECT `call_session`.`id`,
(SELECT `name` FROM `continue_reason` WHERE id = `call_session`.`continue_reason_id`) AS `continue_reason.name`
FROM `call_session`
ORDER BY `call_session`.`start_time` DESC LIMIT 1;
Explain results:
I found many similar questions but I don't have any DISTINCT, no GROUP BY, and there is a very clear benefit for using the index.
Any thoughts?
Thanks in advance
P.S. Force index didn't help.
Here is the Optimizer report

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

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.