【问题标题】:Laravel "foreign key constraint fails"Laravel“外键约束失败”
【发布时间】:2016-08-17 17:04:31
【问题描述】:

工作模型中的关系

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
    ]);

//error 'account_id' 外键约束失败

创建表作业

$table->integer('account_id')->unsigned()->index();
$table->foreign('account_id')->references('id')->on('ebay_accounts')->onDelete('cascade');

shchema 数据库

写了

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

【问题讨论】:

  • 给出表的架构!

标签: laravel foreign-keys relationship


【解决方案1】:

我认为问题出在您的“工作”模型中。您是否在工作模型中设置了$fillable。尝试将 'account_id, action, days' 添加到 $fillable 数组中,如下所示:

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

这将为模型启用批量分配到您的数据库。 https://laravel.com/docs/5.2/eloquent#mass-assignment

【讨论】:

  • alredy write protected $fillable = [ 'account_id','action', 'priority', 'pagination', 'p2', 'p3', 'days','day_start', 'day_end' , '完成' ];
猜你喜欢
  • 1970-01-01
  • 2019-05-23
  • 2016-01-20
  • 2013-05-29
  • 1970-01-01
  • 2013-03-04
  • 2011-11-03
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多