Reputation: 374
I want to to make a relation between company model and jobs model But it gives me this error:
PDOException::("SQLSTATE[HY000]: General error: 1005 Can't create table `my-career`.`#sql-2fd8_ba` (errno: 150 "Foreign key constraint is incorrectly formed")")
Company Model:
class Company extends Model{
public $table="comppanies";
public function jobs() {
return $this->hasMany(App\Job::class);
}
}
Job Model:
class Job extends Model{
public $table="jobs";
public function company() {
return $this->belongsTo(App\Company::class, 'company_id');
}
}
Job Table:
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');
});
}
I don't know where is the problem
Upvotes: 3
Views: 270
Reputation: 67505
You should use table name comppanies
in the reference source, so :
$table->foreign('company_id')->references('id')->on('company');
Should be :
$table->foreign('company_id')->references('id')->on('comppanies');
_____________________________________________________^^^^^^^^^^
Upvotes: 3