s. It is possible to do a Delete Cascade
of child records in the same table. I found this post in the MYSQL forums had the answer. Here's how I got it to work.
- I had to make sure that the primary id's parent was set to NULL.
- I had to make sure that the primary id and parent id's were set to exactly the same kind of field, such as INT.
- I also had to make sure the primary id was set to auto increment.
From the MYSQL forum:
create table edges(
ID int PRIMARY KEY,
parentid int,
unique key(id, parentid),
foreign key(parentID) references edges(ID) ON DELETE CASCADE ON UPDATE CASCADE
) engine=innodb;
insert into edges(ID,parentID) values (1,null),(2,1),(3,1),(4,2);
Now do this and watch the parent and all children delete cascade:
delete from edges where id=2;
select * from edges will then show two records left. Record 1 and record 3.
This was huge for my project, a gallery where users could create albums inside of albums inside of albums.