Cho*_*ier 1 migration postgresql 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(){
return $this->hasOne('App\job_search');
}
Run Code Online (Sandbox Code Playgroud)
将迁移更改为:
public function up()
{
Schema::create('job_details', function (Blueprint $table) {
$table->increments('id');
$table->unsignedInteger('job_search_id');
});
Schema::table('job_details', function (Blueprint $table) {
$table->foreign('job_search_id')->references('id')->on('job_searches');
});
}
Run Code Online (Sandbox Code Playgroud)
和JobSearch班级:
class JobSearch extends Model
{
public function jobDetails(){
return $this->hasMany('App\JobDetail');
}
}
Run Code Online (Sandbox Code Playgroud)
和JobDetail班级:
class JobDetails extends Model
{
public function jobSearch()
{
return $this->belongsTo('App\JobSearch');
}
}
Run Code Online (Sandbox Code Playgroud)
如果您不加修改地使用代码,它将为您工作。