Laravel"外键约束失败"

时间:2016-04-24 09:29:32

标签: laravel foreign-keys relationship

工作模式中的关系

public function account()
{
    return $this->belongsTo(EbayAccount::class, 'id', 'account_id');
}
EbayAccount中的关系

public function jobs()
{
    return $this->hasMany(Job::class, 'account_id', 'id');
}

主要代码 //那个工作

        $job = new Job();
        $job->account_id = $acc->id;
        $job->action = 'getOrders';
        $job->days = 30;
        $job->save();

//无效

     Job::create([ 
        'account_id' => $acc->id,
        'action' => 'getOrders',
        'days' => 30
    ]);

//错误' account_id'外键约束失败

创建表作业

$table->integer('account_id')->unsigned()->index();
$table->foreign('account_id')->references('id')->on('ebay_accounts')->onDelete('cascade');
shchema db enter image description here

写了

 protected $fillable = [
        'account_id','action', 'priority', 'pagination', 'p2', 'p3', 'days','day_start', 'day_end', 'done'
    ];

1 个答案:

答案 0 :(得分:0)

I think the problem is in your "Job" Model. Have you set the $fillable in your Job Model. Try adding 'account_id, action, days' to the $fillable array like this:

protected $fillable = ['account_id', 'action', 'days'];

This will enable mass assignment to your database for the model. https://laravel.com/docs/5.2/eloquent#mass-assignment