Welcome Guest, Not a member yet? Register   Sign In
Cannot add or update a child row: a foreign key constraint fails
#3

(This post was last modified: 06-09-2022, 10:24 AM by Sincere.)

(06-08-2022, 02:07 PM)iRedds Wrote: Check if the fields in $allowedFields match.
Try adding the same data via a raw query.

[Image: 2fx]

Works fine if I run the query manually through PHPmyadmin.

Model looks fine to me:
Code:
{
    protected $DBGroup = 'default';
    protected $table      = 'live_outputs';
    protected $primaryKey = 'id';

    protected $useAutoIncrement = true;
    protected $returnType    = 'object';
    protected $useSoftDeletes = true;

    protected $allowedFields = ['alias','status','description','livestream_id,','type','secret','max_resolution'];

    protected $useTimestamps = true;
    protected $createdField  = 'created_at';
    protected $updatedField  = 'updated_at';
    protected $deletedField  = 'deleted_at';

    protected $validationRules    = [
        'alias'        => 'required|min_length[4]|max_length[16]|alpha_numeric|is_unique[live_outputs.alias]',
        'status'        =>  'numeric',
        'description'        => 'required|max_length[40]',
        'livestream_id'      =>  'required|numeric',
        'type'    =>  'required|max_length[8]',
        'secret'        => 'permit_empty|max_length[16]',
        'max_resolution'        =>  'permit_empty|numeric',
    ];
    protected $skipValidation    = false;
}
If I run the query manually from CI works fine as well:
PHP Code:
    public function test_insert()
    {
        $data=  [
            'alias' =>  'AbCdEfGh',
            'status'    =>  0,
            'description'  =>  'Manual SQL Test',
            'livestream_id' =>  5,
            'type'  =>  'public'
        ];
        $db=    \Config\Database::connect();
        $builder    =  $db->table('live_outputs');
        $builder->insert($data);
    

[Image: 2fy]

So apparently something goes wrong when using the model/controller:

PHP Code:
    public function add()
    {
        $authorize service('authorization');
        $LiveOutputModel = new LiveOutputModel();

        $LiveModel = new LiveModel();

        //removed some authentication code

        if(!$this->can_add_live()){return(\CodeIgniter\Exceptions\PageNotFoundException::forPageNotFound());}

        if($_SERVER['REQUEST_METHOD'] == 'POST') {
            $_POST['alias'] = $this->getKey(8);
            $LiveOutputModel->insert($_POST);
            if($LiveOutputModel->errors()==null){
                return(redirect()->to('/panel/live/outputs/'));
            }
// load view, helper etc.
        
Reply


Messages In This Thread
RE: Cannot add or update a child row: a foreign key constraint fails - by Sincere - 06-09-2022, 05:49 AM



Theme © iAndrew 2016 - Forum software by © MyBB