M
M
Mikhail2017-06-26 21:03:32
PHP
Mikhail, 2017-06-26 21:03:32

How to properly set up cascading deletion?

Hello. There are three tables:
cdd4a6967223490298f660290d2b47b0.png
Table links main
I get id. It is necessary to delete all records from links, where the telegramID = id field and all related data from the plants table. Tried via cascade delete like so:

alter table links add constraint auto_del
foreign key (id)
references plants(id) on delete cascade;

But when you delete data from links in the plants table, everything remains unchanged. How to be?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
D
DanKud, 2018-12-01
@rusgayfer

$str = '1502_0,1510_1,628_2,5293_3,6650_4,5308_5,3187_6,3207_7';
$list = explode(',', $str);
$i = 0;

foreach ($list as $item) {
  $data = explode('_', $item);
  $arr[$i] = $data;
  $i++;
}

print_r($arr);

B
Boris Korobkov, 2017-06-26
@mak_ufo

alter table links add constraint auto_del
foreign key (id)
references plants(id) on delete cascade;

This says that when deleting from plants, it should also delete from links, not vice versa!
When removed from links, it cannot be automatically removed from plants. And it's illogical to do so. For it is quite possible to remove one of the many-to-many values, leaving all the others.
If you have many-to-one relationships, then the structure is wrong. And in general, this is some kind of govnokod. It's not clear from the name what kind of entity it is.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question