How to resolve SQLSTATE[HY000]: General error: 1824 Failed to open the referenced table in laravel?
How to resolve SQLSTATE[HY000]: General error: 1824 Failed to open the referenced table in laravel?
我创建了三个迁移tables user_container table用于存储用户详细信息,admin_table用于存储管理员详细信息,blog_table用于存储博客 .admin 可以创建博客,这就是为什么我为管理员与博客 [=29=] 建立外键关系。当我尝试迁移 tables 时,出现以下错误
Illuminate\Database\QueryException
SQLSTATE[HY000]: General error: 1824 Failed to open the referenced table 'admin_table' (SQL: alter table `blogs_table` add constraint `blogs_table_admin_id_foreign` foreign key (`admin_id`) references `admin_table` (`id`))
请帮我解决这个问题我不知道我错在哪里..
迁移table结构
2021_08_11_170129_create_Blogs_table.php
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
class CreateBlogsTable extends Migration
{
/**
* Run the migrations.
*/
public function up(): void
{
Schema::create('blogs_table', function (Blueprint $table) {
$table->id();
$table->string('name');
$table->string('price');
$table->string('country');
$table->longText('description');
$table->integer('rating');
$table->longText('image');
$table->unsignedInteger('admin_id');
$table->foreign('admin_id')->references('id')->on('admin_table');
$table->timestamps();
//$table->softDeletes();
});
}
/**
* Reverse the migrations.
*/
public function down(): void
{
Schema::dropIfExists('blogs_table');
}
}
2021_08_12_121933_create_admin_table.php
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
class CreateAdminTable extends Migration
{
/**
* Run the migrations.
*/
public function up(): void
{
Schema::create('admin_table', function (Blueprint $table) {
$table->id();
$table->string('firstName');
$table->string('lastName');
$table->string('email')->unique();
$table->string('mobile');
$table->string('password');
$table->timestamps();
//$table->softDeletes();
});
}
/**
* Reverse the migrations.
*/
public function down(): void
{
Schema::dropIfExists('admin_table');
}
}
您正在尝试 运行 首先在您的博客 Table 上迁移,然后,您 运行 在管理员 Table 上迁移。
Laravel migration 使用文件开头的时间戳来决定应该首先迁移哪个 migration。
确保在博客 table 之前先创建一个管理员 table(这也适用于任何有引用的 table)。或者只是重命名文件(更改时间戳),例如:
2021_08_12_121933_create_admin_table.php
2021_08_11_170129_create_Blogs_table.php
为此:
2021_08_11_121933_create_admin_table.php
2021_08_12_170129_create_Blogs_table.php
然后 运行 php artisan migrate:fresh
刷新迁移。
当您设置外键时 $table->foreign('admin_id')->references('id')->on('admin_table');
table admin_table 还不存在。
将 admin_table 的迁移名称更改为 运行 之前的博客名称。
2021_08_11_121933_create_admin_table.php
而不是
2021_08_12_121933_create_admin_table.php
我创建了三个迁移tables user_container table用于存储用户详细信息,admin_table用于存储管理员详细信息,blog_table用于存储博客 .admin 可以创建博客,这就是为什么我为管理员与博客 [=29=] 建立外键关系。当我尝试迁移 tables 时,出现以下错误
Illuminate\Database\QueryException
SQLSTATE[HY000]: General error: 1824 Failed to open the referenced table 'admin_table' (SQL: alter table `blogs_table` add constraint `blogs_table_admin_id_foreign` foreign key (`admin_id`) references `admin_table` (`id`))
请帮我解决这个问题我不知道我错在哪里..
迁移table结构
2021_08_11_170129_create_Blogs_table.php
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
class CreateBlogsTable extends Migration
{
/**
* Run the migrations.
*/
public function up(): void
{
Schema::create('blogs_table', function (Blueprint $table) {
$table->id();
$table->string('name');
$table->string('price');
$table->string('country');
$table->longText('description');
$table->integer('rating');
$table->longText('image');
$table->unsignedInteger('admin_id');
$table->foreign('admin_id')->references('id')->on('admin_table');
$table->timestamps();
//$table->softDeletes();
});
}
/**
* Reverse the migrations.
*/
public function down(): void
{
Schema::dropIfExists('blogs_table');
}
}
2021_08_12_121933_create_admin_table.php
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
class CreateAdminTable extends Migration
{
/**
* Run the migrations.
*/
public function up(): void
{
Schema::create('admin_table', function (Blueprint $table) {
$table->id();
$table->string('firstName');
$table->string('lastName');
$table->string('email')->unique();
$table->string('mobile');
$table->string('password');
$table->timestamps();
//$table->softDeletes();
});
}
/**
* Reverse the migrations.
*/
public function down(): void
{
Schema::dropIfExists('admin_table');
}
}
您正在尝试 运行 首先在您的博客 Table 上迁移,然后,您 运行 在管理员 Table 上迁移。
Laravel migration 使用文件开头的时间戳来决定应该首先迁移哪个 migration。
确保在博客 table 之前先创建一个管理员 table(这也适用于任何有引用的 table)。或者只是重命名文件(更改时间戳),例如:
2021_08_12_121933_create_admin_table.php
2021_08_11_170129_create_Blogs_table.php
为此:
2021_08_11_121933_create_admin_table.php
2021_08_12_170129_create_Blogs_table.php
然后 运行 php artisan migrate:fresh
刷新迁移。
当您设置外键时 $table->foreign('admin_id')->references('id')->on('admin_table');
table admin_table 还不存在。
将 admin_table 的迁移名称更改为 运行 之前的博客名称。
2021_08_11_121933_create_admin_table.php
而不是
2021_08_12_121933_create_admin_table.php