I'm trying to make an database and one of my tables use composite primary keys. This works fine until I try to make a foreign key to a separate table using the 2nd composite key. Error given is "ERROR 1005 (HY000): Can't create table '.kesharaproducts\production.frm' (errno: 150)"
Here's the code.
create table stocks(
rawMatBatchID VARCHAR(4) NOT NULL,
finishedGoodsBatchID VARCHAR(4) NOT NULL,
rawMaterialID VARCHAR(4) NOT NULL,
finishedMaterialID VARCHAR(4) NOT NULL,
supplierID VARCHAR(5) NOT NULL,
rawMaterialType VARCHAR(10) NOT NULL,
finishedMaterialType VARCHAR(10) NOT NULL,
rawMatWeight VARCHAR(5) NOT NULL,
finishedGoodsWeightKg INT(5) NOT NULL,
finishedGoodsWeightG INT(5) NOT NULL,
finishedGoodsUnits INT(5) NOT NULL,
finishedGoodsDate VARCHAR(15) NOT NULL,
rawMatDate VARCHAR(15) NOT NULL,
PRIMARY KEY (finishedGoodsBatchID, rawMatBatchID),
CONSTRAINT FOREIGN KEY (finishedMaterialID) REFERENCES finishedMaterials(finishedMaterialID),
CONSTRAINT FOREIGN KEY (supplierID) REFERENCES supplierDetails(supplierID),
CONSTRAINT FOREIGN KEY (rawMaterialID) REFERENCES rawMaterials(rawMaterialID))ENGINE=INNODB;
The table above use the composite primary key.
create table transport(
transportID VARCHAR(4) NOT NULL,
vehicleID VARCHAR(4) NOT NULL,
finishedGoodsBatchID VARCHAR(4) NOT NULL,
finishedGoodsUnits INT(5) NOT NULL,
finishedGoodsWeightKg INT(5),
finishedGoodsWeightG INT(5),
transportDate VARCHAR(15),
CONSTRAINT PRIMARY KEY (transportID),
CONSTRAINT FOREIGN KEY (vehicleID) REFERENCES vehicles(vehicleID),
CONSTRAINT FOREIGN KEY (finishedGoodsBatchID) REFERENCES stocks(finishedGoodsBatchID)
)ENGINE=INNODB;
Table above used the first of the composite keys and works fine.
create table production(
productionBatchID VARCHAR(4) NOT NULL,
finishedMaterialID VARCHAR(4) NOT NULL,
rawMaterialID VARCHAR(4) NOT NULL,
productionDate VARCHAR(15),
rawMatBatchID VARCHAR(4),
initialWeight INT(5),
beforeWeight INT(5),
afterWeight INT(5),
finalWeight INT(5),
packingWeight INT(5),
noOfUnits INT(5),
wastage INT(5),
CONSTRAINT PRIMARY KEY (productionBatchID),
CONSTRAINT FOREIGN KEY (finishedMaterialID) REFERENCES finishedMaterials(finishedMaterialID),
CONSTRAINT FOREIGN KEY (rawMaterialID) REFERENCES rawMaterials(rawMaterialID),
CONSTRAINT FOREIGN KEY (rawMatBatchID) REFERENCES stocks(rawMatBatchID))ENGINE=INNODB;
But in the table above, which uses the 2nd composite primary as a foreign key, it gives out the error. How to fix this? These are the last three tables. All the other referred tables are created. I didn't post all the tables here, too long. When I remove "CONSTRAINT FOREIGN KEY (rawMatBatchID) REFERENCES stocks(rawMatBatchID)" from the last table and add the table it gets added. But not with the referral to the 2nd composite key...