Fix foreign key bugs.