Re: onDelete cascade after table has been created

From: Tony Mountifield <tony_at_mountifield.org>
Date: Sat, 20 Feb 2016 15:17:30 +0000 (UTC)
Message-ID: <naa02a$94l$1_at_softins.softins.co.uk>


In article <afdf9d11-31be-4173-ac9c-1adc22f2fef9_at_googlegroups.com>,  <apoorv.kanungo_at_gmail.com> wrote:
> Hello I know we can use cascading while creating table but After I created table and there are bunch of data in it how
> would I do ondelete cascade.
>
> I have a user table and a article table with one to many relationship and I have a foreign key user_id in article which
> refrences id in user table when I ran this query
>
> ALTER TABLE `article`
> FOREIGN KEY (`user_id`) REFERENCES `user` (`id`) ON DELETE CASCADE;
>
> Now i Have foreign key but when I delete an entry in user table the corresponding entry in article does not get deleted.

First thing would be to check the table definitions:

SHOW CREATE TABLE `article`;
SHOW CREATE TABLE `user`;

and make sure the foreign key definition really exists.

Also check what the ENGINE is for those tables. If the engine is MyISAM, then foreign key definitions are accepted but not acted upon. You need the ENGINE to be InnoDB:

ALTER TABLE `article` ENGINE=InnoDB;
ALTER TABLE `user` ENGINE=InnoDB;

Cheers
Tony

-- 
Tony Mountifield
Work: tony_at_softins.co.uk - http://www.softins.co.uk
Play: tony_at_mountifield.org - http://tony.mountifield.org
Received on Sat Feb 20 2016 - 16:17:30 CET

Original text of this message