Search code examples
mysqlforeign-keysmysql-error-1005

Cannot create foreign key in MySQL error: 150


I have created the following tables and have no idea why my foreign key constraint script is not working.

CREATE TABLE IF NOT EXISTS `project` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`project_id` varchar(60) NOT NULL,
`project_name` varchar(500) NOT NULL,
`cons_bal` int(11) NOT NULL,
`non_cons_bal` int(11) NOT NULL,
`budget_head` varchar(100) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=1 ;

And here is my another table:

CREATE TABLE IF NOT EXISTS `project_map` (
`id` int(11) NOT NULL,
`project_id` varchar(60) NOT NULL,
`head` int(11) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

And here is my add constraint line:

alter table project_map add constraint p_map_fk001 foreign key (`project_id`) references project(`project_id`)

Any help would be nice. Thank you.


Solution

  • In the referenced table, there must be an index where the referenced columns are listed as the first columns in the same order.

    Refer: http://dev.mysql.com/doc/refman/5.6/en/innodb-foreign-key-constraints.html

    So try:

    CREATE TABLE `project` (                                                                                                                                                                                                                                                                                                                             
               `id` int(11) NOT NULL AUTO_INCREMENT,                                                                                                                                                                                                                                                                                                              
               `project_id` varchar(60) NOT NULL,                                                                                                                                                                                                                                                                                                                 
               `project_name` varchar(500) NOT NULL,                                                                                                                                                                                                                                                                                                              
               `cons_bal` int(11) NOT NULL,                                                                                                                                                                                                                                                                                                                       
               `non_cons_bal` int(11) NOT NULL,                                                                                                                                                                                                                                                                                                                   
               `budget_head` varchar(100) NOT NULL,                                                                                                                                                                                                                                                                                                               
               PRIMARY KEY (`id`),                                                                                                                                                                                                                                                                                                                                
               KEY `project_id` (`project_id`)                                                                                                                                                                                                                                                                                                                    
             ) ENGINE=InnoDB DEFAULT CHARSET=utf8   
    
    
    CREATE TABLE `project_map` (                                                                                                                                                                                        
                   `id` int(11) NOT NULL,                                                                                                                                                                                            
                   `project_id` varchar(60) NOT NULL,                                                                                                                                                                                
                   `head` int(11) NOT NULL,                                                                                                                                                                                          
                   PRIMARY KEY (`id`),                                                                                                                                                                                               
                   KEY `project_id` (`project_id`)                                                                                                                                                                                   
                 ) ENGINE=InnoDB DEFAULT CHARSET=utf8 
    
    ALTER TABLE `project_map` ADD CONSTRAINT 
    `p_map_fk001` FOREIGN KEY (`project_id`)
    REFERENCES project(`project_id`);