leftJoin Remove

leftJoin Remove

Restful Web ServicesRestful Web Services Posts: 202Questions: 49Answers: 2

Can someone confirm to me whether or not when I delete an item from a database table if any left join items are also supposed to be deleted at the same time? Mine are not. I can't find in the documentation, although it is probably there, if it is supposed to remove join records or not.

Thanks

This question has accepted answers - jump to:

Answers

  • rf1234rf1234 Posts: 2,991Questions: 87Answers: 421
    edited March 2018 Answer ✓

    I am doing left join deletes all the time but it is not Editor that does them. It is the DBMS! Just implement a foreign key and set DELETE CASCADE. If you are using a link table between two tables you can have the link table entry deleted with DELETE CASCADE. But you would have to manually delete the other table. I do this on "postRemove" of the primary table.

    In this example I didn't implement a foreign key relationship in the DBMS so I have to manually delete the left joined record:

    ->on( 'postRemove', function ( $editor, $id, $values ) {
                logChange( $editor->db(), 'delete', $id, $values, 'gov_manual_creditor' );
            //delete contracts that have the manual creditor id as a foreign key.
                $editor->db()->raw()
                   ->bind( ':fk', $id )
                   ->exec( 'DELETE FROM contract   
                             WHERE gov_manual_creditor_id = :fk' );
            } )       
    
  • rf1234rf1234 Posts: 2,991Questions: 87Answers: 421
    Answer ✓

    Found this one too. Allan confirms that Editor shouldn't do left join deletes.
    https://datatables.net/forums/discussion/44547

  • Restful Web ServicesRestful Web Services Posts: 202Questions: 49Answers: 2

    Great, thanks for the information. I have implemented a delete to manually remove the child items using the preRemove event but I might change to DELETE CASCADE as you suggested.

    Thanks

  • allanallan Posts: 63,512Questions: 1Answers: 10,472 Site admin

    Yes, this is something that I feel should be done by the database as well - its faster and more maintainable :).

    Allan

This discussion has been closed.