Isabell Isabell - 4 months ago 7
SQL Question

Cannot add foreign key constraint, (no different datatypes)

I got the error message you can read in the title. So I red, its important that the keys must have the same datatype. They are (check). Also that both must be a key. They are (check). But thats it with advices, all the people wich already asked the question had one of these Problems. So what is mine now? Any Idea what else could cause this error ?

Parent table:

CREATE TABLE `parent` (
`an_id` char(24) NOT NULL,
`stuff` int(10) unsigned NOT NULL DEFAULT '0',
`otherstuff` datetime DEFAULT NULL,
PRIMARY KEY (`an_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;


My table:

CREATE TABLE `child` (
`date` datetime NOT NULL,
`an_id` char(24) NOT NULL,
PRIMARY KEY (`date`,`an_id`),
CONSTRAINT `child` FOREIGN KEY (`an_id`) REFERENCES `parent` (`an_id `)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

Answer

It's a typo on your part. You have an extra space after an_id:

REFERENCES `parent` (`an_id `)