[英]errno: 150 “Foreign key constraint is incorrectly formed” in laravel 5.5
[英]Laravel - errno: 150 Foreign key constraint is incorrectly formed
我想在公司模型和工作模型之間建立關系但它給了我這個錯誤:
PDOException::("SQLSTATE[HY000]: General error: 1005 Can't create table `my-career`.`#sql-2fd8_ba` (errno: 150 "Foreign key constraint is incorrectly formed")")
公司型號:
class Company extends Model{
public $table="comppanies";
public function jobs() {
return $this->hasMany(App\Job::class);
}
}
工作模式:
class Job extends Model{
public $table="jobs";
public function company() {
return $this->belongsTo(App\Company::class, 'company_id');
}
}
工作表:
public function up()
{
Schema::create('jobs', function (Blueprint $table) {
$table->increments('id');
$table->string('name');
$table->integer('company_id')->unsigned();
$table->foreign('company_id')->references('id')->on('company');
});
}
我不知道問題出在哪里
您應該在參考源中使用表名comppanies
,因此:
$table->foreign('company_id')->references('id')->on('company');
應該 :
$table->foreign('company_id')->references('id')->on('comppanies');
_____________________________________________________^^^^^^^^^^
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.