I created the master table with the composite primary key.
parent table structure is as follows:
CREATE TABLE `taskcategory` (
`SiteID` int(10) unsigned NOT NULL DEFAULT 1,
`TaskID` int(10) unsigned NOT NULL AUTO_INCREMENT,
`TaskName` varchar(45) DEFAULT '',
`TaskDescription` varchar(45) DEFAULT '',
`IsInbuild` int(11) DEFAULT '1',
PRIMARY KEY (`TaskID`,`SiteID`)
);
when i am trying to create the table with foreign key with the above parent table reference i am getting ‘can’t create table error no 150’ error . help me to do that.
child table structure as follows:
CREATE TABLE taskdetails (`SiteID` int(10) unsigned NOT NULL DEFAULT '1',
`TaskID` int(10) unsigned NOT NULL DEFAULT '0',
`SubtaskID` int(10) unsigned NOT NULL,
`ScriptName` varchar(255) DEFAULT '',
`FunctionName` varchar(255) DEFAULT '',
`ButtonName` varchar(255) DEFAULT '',
`IsInbuild` int(10) unsigned DEFAULT '1',
`Description` varchar(255) DEFAULT '',
PRIMARY KEY (`SubtaskID`,`TaskID`,`SiteID`),
INDEX (siteid, taskid),
FOREIGN KEY (siteid, taskid)
REFERENCES taskcategory(siteid, taskid)
ON UPDATE CASCADE ON DELETE CASCADE
) ENGINE=INNODB;
help me to resolve it.
From the manual:
So when you add an index in the parent table it works (yes, I tested it):
You have a primary key on those columns already (which means there’s an implicit index), but the order of the columns is important!