常规错误:1824无法打开引用表 [英] General error: 1824 Failed to open the referenced table

查看:1055
本文介绍了常规错误:1824无法打开引用表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用php artisan migration将'books'表的外键与'categories'表一起设置,但是出现以下错误:

I am trying to set foreign key of my 'books' table with 'categories' table using php artisan migrate, but I got the following error:

    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中使用.我希望有人能帮助我.

I really need help with this to use in my mobile app API. I hope someone can help me.

推荐答案

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

The problem is on the migration itself. Have a look carefully at this

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这样的GUI,您将能够看到它.

You are trying to open the categories table but it basically wasn't there or wasn't created yet. If you use GUI like HeidiSQL or Navicat, or PMA, You will be able to see it.

Laravel迁移以文件开头的时间戳为依据,以确定应先按顺序迁移哪种迁移.

Laravel migration takes the timestamp on the beginning of the file to decide which migration should be migrated first in sequence.

确保首先在books表之前创建一个category表(这也适用于所有有参考的表).或者只是简单地重命名文件(更改时间戳),例如:

Make sure you create a the categories table first before the books table (this also applies for any tables that has reference). Or simply just rename the file (change the timestamp) like E.g:

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 刷新您的迁移.

Then run php artisan migrate:fresh to refresh your migration.

这篇关于常规错误:1824无法打开引用表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆