I am using phpMyAdmin. I have dropped all my tables and recreated them using the export from MySQLWorkbench (this has worked fine before). I have repopulated certain tables with data, but get a failure on this one table with:
#1452 - Cannot add or update a child row: a foreign key constraint fails
(`col8_apps`.`companies`, CONSTRAINT `fk_companies_users1` FOREIGN KEY (`mainadmin`)
REFERENCES `users` (`userid`) ON DELETE NO ACTION ON UPDATE NO ACTION)
The table structures concerned -
companies:
companyid int(10) UNSIGNED AUTO_INCREMENT
client int(10) UNSIGNED
name varchar(150)
mainadmin int(10) UNSIGNED
....
users:
userid int(10) UNSIGNED AUTO_INCREMENT
....
I have a user in users with userid=1
I am trying to run this SQL:
INSERT INTO companies (companyid, client, name, mainadmin, companylogo, active)
VALUES (1,1,'Company One', 1, 'default',1);
If I use phpMyAdmin to Insert, it will let me select the userid from the dropdown list - so it recognises that that foreign key I'm using exists - for mainadmin but it still then gives me the foreign key error above.
Anyone any clues what might be causing this?
EDIT: Running SHOW ENGINE INNODB STATUS gave this curious message:
Trying to add to index `fk_companies_users1_idx` tuple:
DATA TUPLE: 2 fields;
0: len 4; hex 00000001; asc ;;
1: len 4; hex 00000001; asc ;;
But the parent table `col8_apps`.`users`
or its .ibd file does not currently exist!
The table is definitely there, but I've no idea what a .ibd file is?
EDIT to explain why not a duplicate of another question: There was a solution to this which I have added as an answer below. (this is an issue with mySqlWorkbench, not MySql (or derivative))