我尝试使用php artisan migrate来设置我的‘book’表的外键,但是我得到了以下错误:
Illuminate\Database\QueryException
SQLSTATE[HY000]: General error: 1824 Failed to open the referenced table 'categories' (SQL: alter table `books` add constraint `books_category_id_foreign` foreign key (`category_id`) references `categories` (`id`))
帐簿迁移文件:
public function up()
{
Schema::create('books', function (Blueprint $table) {
$table->increments('id');
$table->integer('category_id')->unsigned();
$table->foreign('category_id')->references('id')->on('categories');
$table->string("image");
$table->string("title");
$table->string("description")->nullable();
$table->string("author");
$table->string("cover");
$table->integer("nod")->nullable();// Number of downloads
$table->integer("rating")->nullable();
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('books');
}
类别迁移文件:
public function up()
{
Schema::create('categories', function (Blueprint $table) {
$table->increments('id');
$table->string("title");
$table->string("image");
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('categories');
}
我真的需要在我的移动应用API中使用这方面的帮助。我希望有人能帮助我。
发布于 2020-11-28 09:20:40
问题出在迁移本身。仔细看看这个
SQLSTATE[HY000]: General error: 1824 Failed to open the referenced table 'categories' (SQL: alter table `books` add constraint `books_category_id_foreign` foreign key (`category_id`) references `categories` (`id`))
您正在尝试打开categories
表,但它基本上不在那里或尚未创建。如果您使用图形用户界面,如HeidiSQL或Navicat,或PMA,您将能够看到它。
Laravel迁移采用文件开头的时间戳来决定应该首先按顺序迁移哪个迁移。
请确保在创建books表之前先创建一个categories表(这也适用于任何有引用的表)。或者只是简单地重命名文件(更改时间戳),例如:
2020_01_01_1234_create_books_table.php
2020_01_01_5678_create_categories_table.php
到这个
2020_01_01_1234_create_categories_table.php
2020_01_01_5678_create_books_table.php
然后运行php artisan migrate:fresh
刷新您的迁移。
发布于 2020-09-23 06:08:45
我从昨天开始就遇到了同样的问题,后来我看到了我的错误,我能够理解问题的原因。要考虑的因素太多了
$table->foreignId('category_id')->constrained('categories');或
$table->foreignId('category_id')->constrained();
我的一个迁移文件示例
public function up()
{
Schema::create('project_details', function (Blueprint $table) {
$table->engine = 'InnoDB';
$table->charset = 'utf8mb4';
$table->collation = 'utf8mb4_unicode_ci';
$table->id();
$table->foreignId('project_id')->constrained()->onDelete('cascade');
$table->string('name', 150)->nullable();
$table->string('description', 600)->nullable();
$table->string('location', 150)->nullable();
$table->integer('completed_percent')->nullable()->default(0);
$table->foreignId('manager_id')->constrained('staffs');
$table->foreignId('sponsor_id')->constrained('sponsors')->nullable();
$table->foreignId('donor_id')->constrained('sponsors')->nullable();
$table->foreignId('mda_id')->constrained('sponsors')->nullable();
});
}
发布于 2020-09-04 16:51:49
在创建category表之前尝试获取图书的类别,而book表无法理解您引用的是什么。
解决方案#1
在图书表格之前声明你的分类表格,只需在迁移文件名中重命名日期即可。类别表必须先创建,然后才能创建账簿表。
解决方案#2
创建category表后创建引用。
从图书迁移中删除$table->foreign('category_id')->references('id')->on('categories');
,并在创建category表后创建引用。
类别迁移文件:
public function up()
{
Schema::create('categories', function (Blueprint $table) {
$table->increments('id');
$table->string("title");
$table->string("image");
$table->timestamps();
});
Schema::table('books', function (Blueprint $table) {
$table->foreign('category_id')->references('id')->on('categories');
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('categories');
}
主要思想是在创建所有表时建立所有关系。另外,使用php artisan migrate:fresh
进行快速擦除和重新创建表
https://stackoverflow.com/questions/60826299
复制相似问题