cannot add second FK : got error can't create table'.jobstatus\#sql-32c_12f2f.frm' (errno:150)

StackOverflow https://stackoverflow.com/questions/13486167

  •  01-12-2021
  •  | 
  •  

Domanda

     CREATE TABLE `job` (
    `jobId` int(11) NOT NULL auto_increment,
    `jobcode` varchar(25) default NULL,
    `jobname` varchar(255) default NULL,
    `location` varchar(255) default NULL,
    `budget` int(10) unsigned default NULL,
    `year_type` varchar(100) default NULL,
    `worklineId` int(11) default NULL,
     PRIMARY KEY  (`jobId`),
     KEY `NewIndex` (`worklineId`),
     FOREIGN KEY (`worklineId`) REFERENCES `workline` (`worklineId`)
     ) TYPE=InnoDB;

     CREATE TABLE `subjob` (
    `subjobId` int(11) NOT NULL auto_increment,
    `subjobcode` varchar(25) default NULL,
    `subjobname` varchar(255) default NULL,
    `subjobbudget` int(11) unsigned default NULL,
    `jobgoal_date` date default '0000-00-00',
    `jobId` int(11) default NULL,
     PRIMARY KEY  (`subjobId`),
     KEY `NewIndex` (`jobId`),
     FOREIGN KEY (`jobId`) REFERENCES `job` (`jobId`)
     ) TYPE=InnoDB;

     CREATE TABLE `contract` (
    `contractId` int(11) NOT NULL auto_increment,
    `contractcode` varchar(25) default NULL,
    `price` int(11) unsigned default NULL,
    `contractprice` int(11) unsigned default NULL,
    `company` varchar(50) default NULL,
    `signdate` date default '0000-00-00',
    `begindate` date default '0000-00-00',
    `enddateplan` date default '0000-00-00',
    `note` text,
     PRIMARY KEY  (`contractId`)
     ) TYPE=InnoDB;


     CREATE TABLE `subjob_contract` (
    `subjobcontractId` int(11) NOT NULL auto_increment,
    `status` varchar(11) default NULL,
    `contractId` int(11) default NULL,
    `subjobId` int(11) default NULL,
     PRIMARY KEY  (`subjobcontractId`),
     KEY `NewIndex` (`contractId`),
     KEY `NewIndex2` (`subjobId`),
     FOREIGN KEY (`contractId`) REFERENCES `contract` (`contractId`)
     ) TYPE=InnoDB

I m using mysql front 3.2 to manage database,I can add first fk but when i add second fk i got an error following this : sql execution error #1005. response from the database: can't create table'.jobstatus#sql-32c_12f2f.frm' (errno:150). i already define the new index for fk subjobId reference to subjob table what could be the possibility of this error? thank you

È stato utile?

Soluzione

Check the datatype and size of the subjobId column on primary table and referenced table. both must be same than it will allow you to create foreign key.

Altri suggerimenti

Answer is: You can not refer that column/table which is not created yet. Try to execute tables having foreign keys after the referenced tables.

Obviously you should have consistency in datatypes of foreign key and referenced column as well

Correct Execution Demo. Also You should use Engine=InnoDB instead of Type=InnoDB

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top