I am trying to puzzle a very unusual situation with a foreign key constraint failing despite an existing value in referenced table. My tables:
USERS:
CREATE TABLE `users` (
`id` INT(11) UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
`first_name` VARCHAR(100) NOT NULL,
`last_name` VARCHAR(100) NOT NULL,
`email` VARCHAR(30) NOT NULL,
`address` VARCHAR(100) NOT NULL,
`phone` VARCHAR(50) NOT NULL,
`year_of_birth` INT(4) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
USER_ORDERS:
CREATE TABLE `user_orders` (
`order_id` INT(11) UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
`user_id` INT(11) UNSIGNED NULL,
`start_date` DATETIME DEFAULT CURRENT_TIMESTAMP,
`due_date` DATE NULL,
CONSTRAINT `fk10_orders_books` FOREIGN KEY (`user_id`) REFERENCES `users` (`id`)
ON UPDATE CASCADE ON DELETE SET NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
BOOKS:
CREATE TABLE `books` (
`id` INT(11) UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
`title` VARCHAR(100) NOT NULL,
`condition` ENUM('mint', 'new', 'medium', 'poor', 'needs replacement'),
`date_added` DATE
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
BOOK_ORDERS: many to many with the aforementioned tables
CREATE TABLE `books_orders` (
`order_id` INT(11) UNSIGNED NOT NULL,
`book_id` INT(11) UNSIGNED NOT NULL,
UNIQUE KEY(`order_id`, `book_id`),
CONSTRAINT `fk_orders_user_orders` FOREIGN KEY (`order_id`) REFERENCES `user_orders` (`order_id`)
ON UPDATE CASCADE ON DELETE CASCADE,
CONSTRAINT `fk_orders_books` FOREIGN KEY (`book_id`) REFERENCES `books` (`id`)
ON UPDATE CASCADE ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
Now, when trying to run the query below
INSERT INTO `books_orders` (`order_id`, `book_id`) VALUES (1, 1);
I am getting
ERROR 1452 (23000): Cannot add or update a child row: a foreign key constraint fails (library.books_orders, CONSTRAINT fk_orders_user_orders FOREIGN KEY (order_id) REFERENCES user_orders (order_id) ON DELETE CASCADE ON UPDATE CASCADE)
I blamed unique key, removed it, replaced with composite Primary Key, tried to re-seed the parent tables with a different dataset, however, nothing has changed. Where could the problem be?