CodeIgniter Forums
Query Builder Class - Problem with delete vs foreign key constraint - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: CodeIgniter 4 (https://forum.codeigniter.com/forum-28.html)
+--- Forum: CodeIgniter 4 Support (https://forum.codeigniter.com/forum-30.html)
+--- Thread: Query Builder Class - Problem with delete vs foreign key constraint (/thread-76535.html)



Query Builder Class - Problem with delete vs foreign key constraint - George Rangel - 05-21-2020

Hi everyone!

I have this code:
PHP Code:
    /**
     * TODO - Deleta uma permissão do banco de dados
     *
     * @param Int $id
     * @return Bool
     */
    
public static function deletePermission(Int $id): Bool
    
{
        
//define a tabela
        
$builder $_ENV['db']->table(parent::$tabPermissoes);

        
//tenta deletar o item
        
if($builder->delete(['id' => $id]) !== false) return true;
        else return 
false;
    } 

The code is working when [font=SFMono-Regular, Menlo, Monaco, Consolas,]delete function returns !== false[/font] ... and seeing the docs https://codeigniter.com/user_guide/database/query_builder.html?highlight=delete#delete , in case of failure, the delete [font=Tahoma, Verdana, Arial, sans-serif]function would return false. But I'm getting an error, because I have a MySQL foreign key constraint.[/font]

I can fix it checking this foreign key constraint first, but [font=Tahoma, Verdana, Arial, sans-serif]the [font=SFMono-Regular, Menlo, Monaco, Consolas,]delete [font=Tahoma, Verdana, Arial, sans-serif]function would not returning false?[/font][/font][/font]