我有2个表,当我尝试迁移时,返回此错误:
一般错误:1005无法创建表
usee_anbari
。#sql-473_21177
(错误号:150“外键约束格式不正确”)(SQL:更改表companies
添加约束{{ 1}}外键(companies_access_id_foreign
)引用删除级联上的access_id
(accesses
)
这是我的桌子
id
和另一个:
public function up()
{
Schema::create('companies', function (Blueprint $table) {
$table->bigIncrements('id');
$table->string('name');
$table->text('address');
$table->string('tel1');
$table->string('tel2');
$table->integer('owner');
$table->unsignedBigInteger('access_id');
$table->string('depot_number')->default(2);
$table->timestamps();
$table->foreign('access_id')->references('id')->on('accesses')
->onDelete('cascade');
});
}
我想念什么?
答案 0 :(得分:0)
答案 1 :(得分:0)
我遇到了同样的问题,我在这里描述如何解决它。
您必须使用unsignedBigInteger
而不是unsignedInteger
。请参见以下代码:
Schema::create('orders', function (Blueprint $table) {
$table->bigIncrements('id');
$table->unsignedBigInteger('user_id')->nullable(); // Changed here
$table->string('ip_address')->nullable();
$table->string('email')->nullable();
$table->text('message')->nullable();
$table->timestamps();
$table->foreign('user_id')
->references('id')->on('users')
->onDelete('cascade');
});
在将使用foreign
的地方使用unsignedBigInteger
(如果有ID)。如果仍然不清楚,请发表评论。