小编Cho*_*ier的帖子

一对多关系 Laravel 迁移

我目前有一个 1:1 的关系,我需要它是一对多的关系。这样Job Details可以有多个结果 1 Job Search

求职

public function up()
{
    Schema::create('job_searches', function (Blueprint $table) {
        $table->increments('id');
   });
}
Run Code Online (Sandbox Code Playgroud)

工作详情

public function up()
{
    Schema::create('job_details', function (Blueprint $table) {
        $table->integer('job_details_id',11);
        $table->foreign('job_details_id')->references('id')->on('job_searches');    
    });
}
Run Code Online (Sandbox Code Playgroud)

我得到的当前输出是:

Job_Search_Id   
1

Job_Detail_Id
1
Run Code Online (Sandbox Code Playgroud)

将另一个结果添加到job details我得到:

Illuminate\Database\QueryException 带有消息“SQLSTATE[23503]:外键违规:7 错误:插入或更新表“job_details”违反外键约束“job_details_job_details_id_foreign”

我也已经说明了我的模型中的关系

求职模式

class JobSearches extends Model
{
    protected $primaryKey = 'id';

    public function job_details(){
          return $this->belongsToMany('App\job_details');
    }
}
Run Code Online (Sandbox Code Playgroud)

工作详情模型

class JobDetails extends Model
{
 protected $primaryKey = 'job_details_id';

 public function job_search(){ …
Run Code Online (Sandbox Code Playgroud)

migration postgresql laravel

1
推荐指数
1
解决办法
9938
查看次数

标签 统计

laravel ×1

migration ×1

postgresql ×1