首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >常规错误: 1824无法打开被引用的表

常规错误: 1824无法打开被引用的表
EN

Stack Overflow用户
提问于 2020-03-24 15:05:05
回答 7查看 14.8K关注 0票数 11

我尝试使用php artisan migrate来设置我的‘book’表的外键,但是我得到了以下错误:

代码语言:javascript
运行
复制
    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`))

帐簿迁移文件:

代码语言:javascript
运行
复制
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');
}

类别迁移文件:

代码语言:javascript
运行
复制
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中使用这方面的帮助。我希望有人能帮助我。

EN

回答 7

Stack Overflow用户

发布于 2020-11-28 09:20:40

问题出在迁移本身。仔细看看这个

代码语言:javascript
运行
复制
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表(这也适用于任何有引用的表)。或者只是简单地重命名文件(更改时间戳),例如:

代码语言:javascript
运行
复制
2020_01_01_1234_create_books_table.php
2020_01_01_5678_create_categories_table.php

到这个

代码语言:javascript
运行
复制
2020_01_01_1234_create_categories_table.php
2020_01_01_5678_create_books_table.php

然后运行php artisan migrate:fresh刷新您的迁移。

票数 23
EN

Stack Overflow用户

发布于 2020-09-23 06:08:45

我从昨天开始就遇到了同样的问题,后来我看到了我的错误,我能够理解问题的原因。要考虑的因素太多了

  1. 确保父表(categories)的日期早于子表(books)的日期,以便在迁移过程中首先创建父表,因为子表可能要引用不存在的表中的id。
  2. 确保遵循naming
  • you的约定可以像这样重构迁移文件

$table->foreignId('category_id')->constrained('categories');

$table->foreignId('category_id')->constrained();

我的一个迁移文件示例

代码语言:javascript
运行
复制
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();
 });
}
票数 5
EN

Stack Overflow用户

发布于 2020-09-04 16:51:49

在创建category表之前尝试获取图书的类别,而book表无法理解您引用的是什么。

解决方案#1

在图书表格之前声明你的分类表格,只需在迁移文件名中重命名日期即可。类别表必须先创建,然后才能创建账簿表。

解决方案#2

创建category表后创建引用。

从图书迁移中删除$table->foreign('category_id')->references('id')->on('categories');,并在创建category表后创建引用。

类别迁移文件:

代码语言:javascript
运行
复制
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进行快速擦除和重新创建表

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/60826299

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档