Creating MariaDB table with a foreignkey to varchar


Creating MariaDB table with a foreignkey to varchar



I am trying to create a table (yTable) which has a foreign key mapped to another table (xTable). I am using MariaDB.



When I try to create it I get this error:



Cannot find an index in the referenced table where the referenced
columns appear as the first columns, or column types in the table and
the referenced table do not match for constraint.



When i dump existing table it gives me this description:


CREATE TABLE `xTable` (
`id` varchar(25) NOT NULL,
...
PRIMARY KEY (`id`),
UNIQUE KEY `id` (`id`),



...



The query that I try to use to create another table is:


Create table `yTable` (
`xTable_id` varchar(25) NOT NULL,
Primary Key (`xTable_id`),
Foreign Key (`xTable_id`)
references `xTable` (`id`) on delete cascade on update cascade);



In both pasted queries I have removed some columns. They don't affect anything. The problem is with connecting the xTable_id from yTable to xTable.id.
For some reason it doesn't let me create the table even though the columns match.



When I try to create it in Navicat it gives me:



[Err] 1005 - Can't create table 'mydb.yTable' (errno:
150)



Does any have any ideas what might be wrong?









By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

How to add background colour in existing image using Swift?

Moria Casán

How to make file upload 'Required' in Contact Form 7?