I have two EXISTING mysql/mariadb tables such as:
CREATE TABLE `ice_product` (
`id` bigint(20) NOT NULL AUTO_INCREMENT,
`long_description` text DEFAULT NULL,
`long_summary_descr` text DEFAULT NULL,
`name` varchar(255) DEFAULT NULL,
`quality` varchar(255) DEFAULT NULL,
`release_date` date DEFAULT NULL,
`update_date` date DEFAULT NULL,
`short_description` text DEFAULT NULL,
`short_summary_descr` text DEFAULT NULL,
`title` varchar(255) DEFAULT NULL,
`category_id` bigint(20) DEFAULT NULL,
`supplier_id` bigint(20) DEFAULT NULL,
`updated` bit(1) NOT NULL,
`product_family_id` bigint(20) DEFAULT NULL,
PRIMARY KEY (`id`),
KEY `idx_ice_product_cat` (`category_id`),
KEY `idx_ice_product_supp` (`supplier_id`),
KEY `idx_ice_product_updated` (`updated`),
CONSTRAINT `fk_icecat_product_cat_id` FOREIGN KEY (`category_id`) REFERENCES `ice_category` (`id`),
CONSTRAINT `fk_icecat_product_supp_id` FOREIGN KEY (`supplier_id`) REFERENCES `ice_supplier` (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci
and
CREATE TABLE `ice_product_manuals` (
`product_id` bigint(20) NOT NULL,
`descr` varchar(255) DEFAULT NULL,
`url` varchar(800) DEFAULT NULL,
`id` bigint(20) NOT NULL AUTO_INCREMENT,
PRIMARY KEY (`id`),
UNIQUE KEY `unique_manual` (`product_id`,`url`) USING HASH
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci
I wish to define a foreign key constraint
ALTER TABLE ice_product_manuals ADD CONSTRAINT fk_icecat_manuals_product_id FOREIGN
KEY (product_id) REFERENCES ice_product(`id`);
but the statement fails with
ERROR 1005 (HY000): Can't create table `mydb`.`ice_product_manuals` (errno: 150 "Foreign key constraint is incorrectly formed")
and SHOW ENGINE INNODB STATUS
shows the following
------------------------
LATEST FOREIGN KEY ERROR
------------------------
2023-04-15 19:05:36 0x7f70e4065640 Error in foreign key constraint of table `mydb`.`ice_product_manuals`:
Alter table `mydb`.`ice_product_manuals` with foreign key `fk_icecat_manuals_product_id` constraint failed. There is no index in the referenced table where the referenced columns appear as the first columns.
- same engine
- same charset and collate
- both table have indexes
- both table are empty
where is the problem?