如何解决SQLSTATE [HY000]:一般错误:1005无法创建表Datacompany_eligibilities错误号:150“外键约束格式不正确”
public function up()
{
Schema::create('company_eligibilities',function (Blueprint $table) {
$table->id();
$table->string('marks');
$table->integer('company_id')->unsigned();
$table->timestamps();
$table->foreign('company_id')
->references('id')->on('companies')
->onDelete('cascade')
->onUpdate('cascade');
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('company_eligibilities');
}
公司迁移已在执行上述迁移之前完成
当我尝试与外键一起创建表时,出现上述错误。我做错什么了吗?
解决方法
请确保在迁移companies
之前先迁移company_eligibilities
,然后将外键ID更改为
无符号大整数,Laravel 5.8将默认外键设置为无符号大整数,对于 6.x 和 7.x :
$table->bigInteger('company_id')->unsigned();
或者,
$table->unsignedBigInteger('company_id');
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。