php - Laravel - errno:150 外键约束的格式不正确
问题描述
我想在公司模型和工作模型之间建立关系但这给了我这个错误:
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');
_____________________________________________________^^^^^^^^^^
推荐阅读
- google-cloud-platform - 如何仅在一个或两个特定的 Google Cloud Compute Engine 实例上授予“编辑”角色?
- tensorflow - tf.estimator.BoostedTreesRegressor SavedModel 恢复问题
- javascript - Reactjs 不理解 ES6?
- javascript - 如果特定块处于活动状态,古腾堡编辑器会检查 JS
- scala - 在 REPL 中除以零时省略了哪些 28 帧?
- html - 100% 宽度/高度绝对父级,带填充
- sql - 是否可以将变量的值连接到接受提示文本中?
- jsf - 如何在 ui:repeat 中获取 h:outputText 嵌套在 h:dataTable 中以在更新数据表时进行更新?
- swift - 给 SCNPlane 添加阴影
- html - 溢出:滚动属性不包括溢出中的背景