Nomadic Posted January 22, 2010 Share Posted January 22, 2010 MySQL server version: 5.1.30 MySQL statement: ALTER TABLE table1 ADD FOREIGN KEY (`g_id`) REFERENCES table2 (`g_id`) ON DELETE CASCADE ON UPDATE CASCADE; Errors that MySQL returns to the client: #1452 - Cannot add or update a child row: a foreign key constraint fails Table structure: CREATE TABLE `table1` ( `g_id` int(10) unsigned NOT NULL, `inee_id` mediumint( unsigned NOT NULL, `iner_id` mediumint( unsigned NOT NULL, PRIMARY KEY (`g_id`,`inee_id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_bin I am trying to add foreign keys to various tables associating with the id's of the primary tables with cascade on update and delete. This is so that when entries get updated or deleted on the main tables it will cascade through and do the same to all the dependant tables without me having to worry about forgetting something. The problem is that I am new to foreign key use in mysql and all the differences from what I am used to has me lost with how to actually input a proper foreign key. Quote Link to comment https://forums.phpfreaks.com/topic/189479-having-trouble-altering-table-to-insert-foreign-keys/ Share on other sites More sharing options...
fenway Posted January 22, 2010 Share Posted January 22, 2010 Same exact column type/definition? Quote Link to comment https://forums.phpfreaks.com/topic/189479-having-trouble-altering-table-to-insert-foreign-keys/#findComment-1000189 Share on other sites More sharing options...
artacus Posted January 22, 2010 Share Posted January 22, 2010 Sounds like your integrity is already broken. Fix it by adding the corresponding rows to table2 or remove the offending rows from table1. Quote Link to comment https://forums.phpfreaks.com/topic/189479-having-trouble-altering-table-to-insert-foreign-keys/#findComment-1000195 Share on other sites More sharing options...
Nomadic Posted January 23, 2010 Author Share Posted January 23, 2010 Same exact column type/definition? Yes the g_id column is the exact same in both tables, both tables are innodb, as far as I can tell it's something wrong with my query not my table structure. Quote Link to comment https://forums.phpfreaks.com/topic/189479-having-trouble-altering-table-to-insert-foreign-keys/#findComment-1000259 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.