add foreign key that ref's column in same table
I'm trying to add a foreign key that references the primary id in the same table.
But when I do so, all I get is a message that " #1215 - Cannot add foreign key constraint".
This is my table definition and the code I'm trying to use to add the fk is below that.
CREATE TABLE IF NOT EXISTS `categories` (
`id` bigint(20) NOT NULL AUTO_INCREMENT,
`name` varchar(48) NOT NULL,
`userID` smallint(5) unsigned NOT NULL,
`type` enum('income','expense') NOT NULL,
`parentID` bigint(20) unsigned DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=102 ;
ALTER TABLE `categories`
ADD KEY `fkParentID` (`parentID`),
ADD CONSTRAINT `fkParentID` FOREIGN KEY (`parentID`)
REFERENCES `categories` (`id`) ON DELETE CASCADE ON UPDATE CASCADE
Don
Subject
Views
Written By
Posted
add foreign key that ref's column in same table
5033
January 01, 2016 11:11AM
1786
January 01, 2016 12:37PM
2385
January 01, 2016 02:13PM
1067
January 01, 2016 03:34PM
1046
January 01, 2016 05:34PM
1000
January 01, 2016 07:53PM
948
January 01, 2016 08:01PM
1088
January 01, 2016 08:37PM
955
January 07, 2016 10:39PM
1133
January 08, 2016 11:38AM
1386
January 07, 2016 09:14PM
Sorry, you can't reply to this topic. It has been closed.
Content reproduced on this site is the property of the respective copyright holders.
It is not reviewed in advance by Oracle and does not necessarily represent the opinion
of Oracle or any other party.