I'm trying to create a table with a foreign key to itself. I want to model a category that can be a child of another category. Here is my SQL:
CREATE TABLE IF NOT EXISTS `recipes`.`category` (
`id` INT NOT NULL COMMENT '',
`name` VARCHAR(60) NOT NULL COMMENT '',
`description` VARCHAR(255) NULL COMMENT 'Description of the recipe category.',
`parent_id` INT NULL COMMENT '',
PRIMARY KEY (`id`) COMMENT '',
CONSTRAINT `parent_id`
FOREIGN KEY (`id`)
REFERENCES `recipes`.`category` (`id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION);
However, MySQL keeps giving me an error:
Can't create table 'recipes.category' (errno: 150)
I'm trying to find out what I'm doing wrong, can anyone give me a hint? Docs say that:
Cannot create table. If the error message refers to error 150, table creation failed because a foreign key constraint was not correctly formed.
However, this does not help me much.
Aucun commentaire:
Enregistrer un commentaire