⚠ In case you've missed it, we have migrated to our new website, with a brand new forum. For more details about the migration you can read our blog post for website migration. This is an archived forum. ⚠

  •     

profile picture

foreign key, does not delete



Joabe Anderson

Joabe Anderson
  • profile picture
  • Member

Posted 28 June 2013 - 14:25 PM

foreign key, does not delete and will not appear error message! Anyone been through this?

 


davidoster

davidoster
  • profile picture
  • Member

Posted 28 June 2013 - 21:42 PM

Can you please post the controller's code and the structure of the tables?


Amit Shah

Amit Shah
  • profile picture
  • Member

Posted 01 July 2013 - 07:12 AM

are your foreign key constraints applied in mysql or you trying to control the same through the code..? if it is there in the mysql .. it should.. you can try and check the same for what is the error @mysql console by executing the deletion of the record / entry. If it dose there, the same effect should be cascaded in here..

If it is managed by code .. you have to manage the code @callback_after_delete .. where you can write in your code to do the required cleanup. Grocery crud wont directly delete the foreign key constraints defined @code level.


kadejo

kadejo
  • profile picture
  • Member

Posted 19 May 2020 - 18:22 PM

Some years has passed since this thread was alive, but that behaviour remains: no message is showed nor action happens when a delete (or an update) try to violate a row referenced from other table with an foreign key ON DELETE/ON UPDATE RESTRICT.

 

So, for the record, the only solution I found is to set the error message in the CRUD function, i.e.:

$crud->set_lang_string('delete_error_message', "My generic delete error message"); 

And make a callback that checks this situation returning if the delete can go ahead:

$crud->callback_before_delete(array($this,'check_fk_table'));

The callback function would be something similar to this:

 function check_fk_table($primary_key) {
        //search for elements in the reference table that are using this value
        $this->db->where('id_reference_table', $primary_key);
        $result = $this->db->get('reference_table');
        $num_rows=$result->num_rows();
        
        if ($num_rows != 0) {
            // Violation of FK ON DELETE RESTRICT
            return false;
        } else {
            // No references, no problem: delete
            return true;
        }
    }