Isabell Isabell - 1 year ago 36
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`)

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 `)

Answer Source

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

REFERENCES `parent` (`an_id `)