Welcome Guest, Not a member yet? Register   Sign In
Foreign Key constraint check
#11

[eluser]ch5i[/eluser]
Thanks noctrum,

the problem is not really the database, it works as it should - prevents deletion of a child row and throws error 1451 (foreign key constraint failure).

Quote:Everything is working fine, just want to know how to detect and display the error with foreign key on delete, rather than display the CI error

The problem is when trying to delete a child row I'd like to tell the user that the child row cannot be deleted because it still has a parent.

You can prevent showing the error messages by disabling all db debug info in db config, but i'd prefer to do it in another way.
#12

[eluser]danmontgomery[/eluser]
Not having done it myself, I would think that the query() call would return false as with any other failed query, so you would just need to check for the return value... No?
#13

[eluser]mestresan[/eluser]
I have this problem...
I have innodb tables with FK with option DELETE RESTRICT, so, cannot delete parent registry. OK.
But, the user can click on delete parent registry, and CI show error.
I dont want show this error from CI. I want return a error like "FALSE" for query...

how !? Is it possible ?
#14

[eluser]InsiteFX[/eluser]
The best way to solve that is to use transactions.

CodeIgniter User Guide - Transactions

Then you can display errors.

InsiteFX
#15

[eluser]jwburnside[/eluser]
I was just having this exact issue today as well. I read up on transactions, but after experimenting with them it seems that they cannot suppress an error message with debugging turned on. Here's my code:

Code:
public function delete_row()
{      
        
        $this->_db_model->trans_start();
        $this->_db_model->delete(self::NAME, array(self::PRIMARY => $this->{"get_" . self::PRIMARY}()));
        $this->_db_model->trans_complete();
            
        if ($this->_db_model->trans_status() === FALSE) {
            return FALSE;
        }
        else {
            return TRUE;
        }
}

Always halts at the SQL error. I'm in an environment where we can't turn off debugging, anyone have any other suggestions? I'm sure I could come up with a workaround, but this is the kind of thing that I will probably see in the future, so I'd like to get some input on the best way to handle it. Thanks, JW

/**Update**/
Upon further review, I probably shouldn't be relying on a DB error to facilitate an action, at least in PHP. I'm just going to run the check beforehand, unless someone has another suggestion.




Theme © iAndrew 2016 - Forum software by © MyBB