Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialMayur Pande
Courses Plus Student 11,711 PointsGetting error code 1005
When trying to create a table with a constraint and foreign key, I keep getting the error message
Error Code: 1005. Can't create table 'londontutors.driver' (errno: 150) 0.074 sec
here are my tables previously
CREATE TABLE `user` (
`email` varchar(80) NOT NULL,
`password` varchar(80) NOT NULL,
`name` varchar(100) NOT NULL,
`address` varchar(200) NOT NULL,
`phone` varchar(50) NOT NULL,
`type` varchar(50) NOT NULL,
PRIMARY KEY (`email`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
CREATE TABLE `grouptuition` (
`tutoremail` varchar(80) NOT NULL,
`starttime` datetime NOT NULL,
`endtime` datetime NOT NULL,
`location` varchar(80) NOT NULL,
`class` varchar(80) NOT NULL,
`level` varchar(80) NOT NULL,
`topic` varchar(200) NOT NULL,
`capacity` int(11) NOT NULL,
PRIMARY KEY (`tutoremail`,`starttime`),
CONSTRAINT `grouptuition_ibfk_1` FOREIGN KEY (`tutoremail`) REFERENCES `user` (`email`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
CREATE TABLE `tutor` (
`email` varchar(80) NOT NULL,
`username` varchar(80) NOT NULL,
`subjects` text NOT NULL,
`summary` text NOT NULL,
`about` text NOT NULL,
`image` varchar(200) NOT NULL,
PRIMARY KEY (`username`),
UNIQUE KEY `username` (`username`),
KEY `email` (`email`),
CONSTRAINT `tutor_ibfk_1` FOREIGN KEY (`email`) REFERENCES `user` (`email`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
and this is the table I am trying to create
create table `driver`(
`driveremail` VARCHAR(80) NOT NULL,
`tutoremail` VARCHAR(80) NOT NULL,
`starttime` DATETIME NOT NULL,
`location` VARCHAR(80) NOT NULL,
`class` VARCHAR(80) NOT NULL,
`topic` VARCHAR(200) NOT NULL,
PRIMARY KEY (`driveremail`),
CONSTRAINT `driver_ibfk_1` FOREIGN KEY (`driveremail`) REFERENCES `tutor` (`email`),
CONSTRAINT `driver_ibfk_2` FOREIGN KEY (`tutoremail`,`starttime`,`location`,`class`,`topic`) references `grouptuition` (`tutoremail`,`starttime`,`location`,`class`,`topic`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
Mayur Pande
Courses Plus Student 11,711 PointsMayur Pande
Courses Plus Student 11,711 Pointssolved it, didn't realise that I with foreign keys you can only reference primary keys of the parent table.