Php 创建迁移时发生SQL外键常规错误1215
我构建了两个迁移:Php 创建迁移时发生SQL外键常规错误1215,php,mysql,laravel,Php,Mysql,Laravel,我构建了两个迁移: public function up() { Schema::create('incidents', function (Blueprint $table) { $table->increments('id'); $table->string('user_id'); $table->integer('incident_type_id')->unsigned(); $table-&g
public function up()
{
Schema::create('incidents', function (Blueprint $table) {
$table->increments('id');
$table->string('user_id');
$table->integer('incident_type_id')->unsigned();
$table->string('type');
$table->string('incident_reference');
$table->integer('incident_id');
$table->date('date');
$table->time('time');
$table->string('location');
$table->string('street');
$table->string('city');
$table->double('latitude', 10, 6);
$table->double('longitude', 10, 6);
$table->smallInteger('incident_archived')->default(0);
$table->timestamps();
});
}
另一个:
public function up()
{
Schema::create('responders', function (Blueprint $table) {
$table->increments('id');
$table->string('user_id');
$table->string('responder_id');
$table->integer('incident_id');
$table->foreign('incident_id')->references('incident_id')->on('incidents');
$table->double('last_lat', 10, 6);
$table->double('last_lng', 10, 6);
$table->timestamps();
});
}
现在我想使用事件id作为事件表的外键。但我得到了一个错误:无法添加外键约束。首先,我认为是迁移的顺序造成了问题,所以应该在事件表之前创建responder表。但事实并非如此。那么,为什么我使用事件id作为外键被阻止
谢谢对不起,我发布了一个答案,但因为我没有正确阅读,所以删除了它。我可以问一下为什么你有事故id和事故id吗?无论哪种方式,我都建议在两次迁移中向
事件id
列添加->unsigned()
。嗨,Joel,我添加了->unsigned()
,但仍然是同一个错误。刚才我想到了-事件表上的事件id
列实际上是索引吗?仅仅把它变成一个整数是不够的,它还必须是一个索引。不。。它不是一个索引。陛下让我试试!基本上是为了性能:“InnoDB需要外键和引用键的索引,这样外键检查可以很快,而不需要扫描表。”(来自文档)