title: Error Code: 1822. Failed to add the foreign key constraint
body: <pre><code>CREATE TABLE `branch` (
    `BranchID` INT NOT NULL, 
    `BranchSuburb` varchar(255) NOT NULL,
    `BranchState` char(3) NOT NULL,
    PRIMARY KEY (`BranchID`)
  ) ENGINE=InnoDB DEFAULT CHARSET=latin1;
  
  CREATE TABLE `member` (
    `MemberID` INT NOT NULL, 
    `MemberStatus` char(9) DEFAULT 'REGULAR',
    `MemberName` varchar(255) NOT NULL,
    `MemberAddress` varchar(255) NOT NULL,
    `MemberSuburb` varchar(25) NOT NULL,
    `MemberState` char(3) NOT NULL,
    `MemberExpDate` DATE,
    `MemberPhone` varchar(10),
    PRIMARY KEY (`MemberID`)
  ) ENGINE=InnoDB DEFAULT CHARSET=latin1;
  
  CREATE TABLE `publisher` (
    `PublisherID` INT NOT NULL, 
    `PublisherName` varchar(255) NOT NULL,
    `PublisherAddress` varchar(255) DEFAULT NULL,
    PRIMARY KEY (`PublisherID`)
  ) ENGINE=InnoDB DEFAULT CHARSET=latin1;
  
  CREATE TABLE `book` (
    `BookID` INT NOT NULL,
    `BookTitle` varchar(255) NOT NULL,
    `PublisherID` INT NOT NULL,
    `PublishedYear` INT4,
    `Price` Numeric(5,2) NOT NULL,
    PRIMARY KEY (`BookID`),
    KEY `PublisherID` (`PublisherID`),
    CONSTRAINT `publisher_fk_1` FOREIGN KEY (`PublisherID`) REFERENCES `publisher` (`PublisherID`) ON DELETE RESTRICT
  ) ENGINE=InnoDB DEFAULT CHARSET=latin1;
  
  CREATE TABLE `author` (
    `AuthorID` INT NOT NULL, 
    `AuthorName` varchar(255) NOT NULL,
    `AuthorAddress` varchar(255) NOT NULL,
    PRIMARY KEY (`AuthorID`)
  ) ENGINE=InnoDB DEFAULT CHARSET=latin1;
  
  CREATE TABLE `authoredby` (
    `BookID` INT NOT NULL,
    `AuthorID` INT NOT NULL, 
    PRIMARY KEY (`BookID`,`AuthorID`),
    KEY `bookID` (`BookID`),
    KEY `AuthorID` (`AuthorID`),
    CONSTRAINT `book_fk_1` FOREIGN KEY (`BookID`) REFERENCES `book` (`bookID`) ON DELETE RESTRICT,
    CONSTRAINT `author_fk_1` FOREIGN KEY (`AuthorID`) REFERENCES `author` (`AuthorID`) ON DELETE RESTRICT
  ) ENGINE=InnoDB DEFAULT CHARSET=latin1;
  
  CREATE TABLE `holding` (
    `BranchID` INT NOT NULL, 
    `BookID` INT NOT NULL,
    `InStock` INT DEFAULT 1,
    `OnLoan` INT DEFAULT 0,
    PRIMARY KEY (`BranchID`,`BookID`),
    KEY `bookID` (`BookID`),
    KEY `BranchID` (`BranchID`),
    CONSTRAINT `holding_cc_1` CHECK(`InStock`&gt;=`OnLoan`),
    CONSTRAINT `book_fk_2` FOREIGN KEY (`BookID`) REFERENCES `book` (`bookID`) ON DELETE RESTRICT,
    CONSTRAINT `branch_fk_1` FOREIGN KEY (`BranchID`) REFERENCES `branch` (`BranchID`) ON DELETE RESTRICT
  ) ENGINE=InnoDB DEFAULT CHARSET=latin1;
  
  
  CREATE TABLE `borrowedby` (
    `BookIssueID` INT UNSIGNED NOT NULL AUTO_INCREMENT,
    `BranchID` INT NOT NULL,
    `BookID` INT NOT NULL,
    `MemberID` INT NOT NULL,
    `DateBorrowed` DATE,
    `DateReturned` DATE DEFAULT NULL,
    `ReturnDueDate` DATE,
    PRIMARY KEY (`BookIssueID`),
    KEY `bookID` (`BookID`),
    KEY `BranchID` (`BranchID`),
    KEY `MemberID` (`MemberID`),
    CONSTRAINT `borrowedby_cc_1` CHECK(`DateBorrowed`&lt;`ReturnDueDate`),
    CONSTRAINT `holding_fk_1` FOREIGN KEY (`BookID`,`BranchID`) REFERENCES `holding` (`BookID`,`BranchID`) ON DELETE RESTRICT,
    CONSTRAINT `member_fk_1` FOREIGN KEY (`MemberID`) REFERENCES `member` (`MemberID`) ON DELETE RESTRICT
  ) ENGINE=InnoDB DEFAULT CHARSET=latin1;
  </code></pre>
  <p>Im getting the following error when i try to execute the last create query for the borrowedby table.
  Error Code: 1822. Failed to add the foreign key constraint. Missing index for constraint 'holding_fk_1' in the referenced table 'holding'</p>