I have two tables, entity
and entity_role
. entity
looks like this:
CREATE TABLE `entity` (
`id` varchar(36) NOT NULL,
`type` varchar(36) NOT NULL,
`name` varchar(150) NOT NULL,
PRIMARY KEY (`id`,`type`)
);
And entity_role
looks like this:
CREATE TABLE `entity_role` (
`id` varchar(36) NOT NULL,
`entity_type` varchar(36) DEFAULT NULL,
`user_id` varchar(36) NOT NULL,
`role_id` varchar(36) NOT NULL,
`entity_id` varchar(36) DEFAULT NULL,
PRIMARY KEY (`id`),
KEY `FK_entity_role_user_id` (`user_id`),
KEY `FK_entity_role_role_id` (`role_id`),
CONSTRAINT `FK_entity_role_role_id` FOREIGN KEY (`role_id`) REFERENCES `role` (`id`),
CONSTRAINT `FK_entity_role_user_id` FOREIGN KEY (`user_id`) REFERENCES `user` (`id`)
);
I'm trying to add a foreign key reference from entity_role
to entity
by running the following statement:
alter table entity_role add constraint `FK_entity_role_entity`
foreign key(`entity_id`, `entity_type`)
references `entity`(`id`, `type`)
on delete cascade;
However I get back from the server:
[Code: 1005, SQL State: HY000] (conn=26) Can't create table entity_role
(errno: 150 "Foreign key constraint is incorrectly formed")
I've looked over this multiple times and read through the MariaDB documentation several times and I feel like I'm banging my head against the wall now. Anyone know what I'm doing wrong here? Both tables are using the InnoDB engine.