DELETE of 2 dependent tables does not work (MariaDB)

Codes here !

Moderators: egami, macek, gesf

Post Reply
iago
php-forum Active User
php-forum Active User
Posts: 36
Joined: Thu Mar 26, 2020 2:33 pm

Mon Apr 27, 2020 5:58 am

Hello,

I have two tables with a foreign key in the child table (sections : cpicks / 1: n)

I tried to delete section entries and dependent cpicks entries like that:
$query = "DELETE sections,cpicks FROM sections INNER JOIN cpicks ON cpicks.section_id = sections.id WHERE sections.id = ".$sectionID."";
However I still receive this message:
Unable to execute query. Cannot delete or update a parent row: a foreign key constraint fails (`cherrypics-3133335250`.`cpicks`, CONSTRAINT `cpicks_ibfk_1` FOREIGN KEY (`section_id`) REFERENCES `sections` (`id`))


What is the reason here? Thanks.
User avatar
hyper
php-forum GURU
php-forum GURU
Posts: 1192
Joined: Mon Feb 22, 2016 5:52 pm

Mon Apr 27, 2020 7:30 am

That's the whole point of using foreign keys, if you don't want or need them, don't use them.
iago
php-forum Active User
php-forum Active User
Posts: 36
Joined: Thu Mar 26, 2020 2:33 pm

Tue Apr 28, 2020 6:38 am

hyper wrote:
Mon Apr 27, 2020 7:30 am
That's the whole point of using foreign keys, if you don't want or need them, don't use them.
O.k., I understand, thanks!
Post Reply