我已经在Laravel迁移中看到了与此错误相关的其他问题,但找不到解决方案。也许我错过了一些基本的东西。
按顺序,我的迁移是:
一个类别有很多sub_categories,它有很多服务。
所以这就是我在迁移文件中写的内容:
分类
class CreateCategoriesTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('categories', function (Blueprint $table) {
$table->increments('id');
$table->timestamps();
$table->string('name', 100);
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('categories');
}
}
子类别:
class CreateSubCategoriesTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('sub_categories', function (Blueprint $table) {
$table->increments('id');
$table->timestamps();
$table->string('name', 100);
$table->integer('category_id')->unsigned();
$table->foreign('category_id')->references('categories')
->on('id')->onDelete('cascade');
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('sub_categories');
}
}
服务:
class CreateServicesTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('services', function (Blueprint $table) {
$table->increments('id');
$table->timestamps();
$table->string('name', 100);
$table->integer('sub_category_id')->unsigned();
$table->foreign('sub_category_id')->references('sub_categories')
->on('id')->onDelete('cascade');
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('services');
}
}
在php artisan migrate
,我得到以下内容:
[Illuminate\Database\QueryException]
SQLSTATE[HY000]: General error: 1005 Can't create table 'servoapp.#sql-b79_34' (errno: 150) (SQL: alter
table `sub_categories` add constraint `sub_categories_category_id_foreign` foreign key (`category_id`)
references `id` (`categories`) on delete cascade)
[PDOException]
SQLSTATE[HY000]: General error: 1005 Can't create table 'servoapp.#sql-b79_34' (errno: 150)
我无法弄清楚我做错了什么。请帮忙!
答案 0 :(得分:2)
更改
$table->foreign('sub_category_id')->references('sub_categories')
->on('id')->onDelete('cascade');
到
$table->foreign('sub_category_id')->references('id')
->on('sub_categories')->onDelete('cascade');
和
$table->foreign('category_id')->references('id')->on('categories')->onDelete('cascade');
像这样的两个模式上的:
子类别:
class CreateSubCategoriesTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('sub_categories', function (Blueprint $table) {
$table->increments('id');
$table->timestamps();
$table->string('name', 100);
$table->integer('category_id')->unsigned();
$table->foreign('category_id')->references('id')
->on('categories')->onDelete('cascade');
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('sub_categories');
}
}
服务
class CreateServicesTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('services', function (Blueprint $table) {
$table->increments('id');
$table->timestamps();
$table->string('name', 100);
$table->integer('sub_category_id')->unsigned();
$table->foreign('sub_category_id')->references('id')
->on('sub_categories')->onDelete('cascade');
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('services');
}
}