Skip to content
Advertisement

Insert with Foreign Key Contraints

In the above code, I want to add the distinct values from column make from vehicles into column makeID in makeModel but I get the error

INSERT INTO makeModel (makeID) SELECT DISTINCT (make) FROM vehicle Error Code: 1452. Cannot add or update a child row: a foreign key constraint fails (`dealership`.`makemodel`, CONSTRAINT `makemodel_ibfk_2` FOREIGN KEY (`modelID`) REFERENCES `model` (`ID`))

Advertisement

Answer

You need to insert into the model field as well, since it’s also a foreign key.

User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement