dazman Posted June 17, 2009 Share Posted June 17, 2009 Hi, this is more MySQL rather than PHP but i am trying to delete a row, and it has a foreign key field in it. This foreign key is linked to another table which has this as its primary key. I get this error message Cannot delete or update a parent row: a foreign key constraint fails () Have I put the FK on the table the wrong way around? I think i added the FK to the child table, rather than the parent. Am i supposed to add it to the parent instead? Quote Link to comment https://forums.phpfreaks.com/topic/162501-force-delete/ Share on other sites More sharing options...
MadTechie Posted June 17, 2009 Share Posted June 17, 2009 Some row in the child does not comply with the constraint, to correct this problem. You find the rows like this: select child.id from child left join parent on (child.parent_id=parent.id) where child.id is not null and parent.id is null; Quote Link to comment https://forums.phpfreaks.com/topic/162501-force-delete/#findComment-857684 Share on other sites More sharing options...
dazman Posted June 17, 2009 Author Share Posted June 17, 2009 Corrected: Hi, this is more MySQL rather than PHP but i am trying to delete a row, and it has a foreign key field in it. This foreign key is linked to another table which has this as its primary key. I get this error message Cannot delete or update a parent row: a foreign key constraint fails () Have I put the FK on the table the wrong way around? I think i added the FK to the parent table, rather than the child. Am i supposed to add it to the child table instead? No rows are coming up.. Im getting an empty set. I ran this statement.. select address.Country from address left join country on (address.Country=country.Country) where country.Country is not null and address.Country is null; In this case address is the parent table, this is the record I am trying to delete. country is the other table with the PK in it. What i was using to delete the record was: DELETE FROM address where Address_ID='ADID00000002'; Do i need to add more to this statement? Quote Link to comment https://forums.phpfreaks.com/topic/162501-force-delete/#findComment-857689 Share on other sites More sharing options...
dazman Posted June 17, 2009 Author Share Posted June 17, 2009 My bad here, i had another table which had this row as a FK. i deleted that record, then i could delete this one... Do you know how to do an if statement to run some code if an empty set is returned? if (empty? (mysql_result("SELECT Address_ID FROM address ORDER BY Address_ID"))) { <code> } Quote Link to comment https://forums.phpfreaks.com/topic/162501-force-delete/#findComment-857699 Share on other sites More sharing options...
MadTechie Posted June 17, 2009 Share Posted June 17, 2009 when you fetch the row it will be empty.. (sorry its 4:30am my bed time) Quote Link to comment https://forums.phpfreaks.com/topic/162501-force-delete/#findComment-857721 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.