Jump to content


foreign key, does not delete

foreign key

  • Please log in to reply
3 replies to this topic

#1 Joabe Anderson

Joabe Anderson

    Newbie

  • Members
  • Pip
  • 4 posts

Posted 28 June 2013 - 02:25 PM

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

 



#2 davidoster

davidoster

    Grocery CRUD Ninja

  • Advanced Member
  • PipPipPip
  • 1,068 posts
  • LocationAthens, Greece

Posted 28 June 2013 - 09:42 PM

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


____________________________________________________________

 

rtfm_small.jpg---!!!Please read these guidelines before asking to the forums!!!---

____________________________________________________________

 

David Oster aka George Pasparakis,
http://odphotography.com
http://eletter.gr


#3 Amit Shah

Amit Shah

    grocery CRUD Master

  • Advanced Member
  • PipPipPip
  • 1,192 posts
  • LocationMumbai, India

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.



#4 kadejo

kadejo

    Newbie

  • Members
  • Pip
  • 3 posts

Posted 19 May 2020 - 06: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;
        }
    }






Also tagged with one or more of these keywords: foreign key

0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users