运行迁移时如何解决外键错误 [英] How to fix foreign key error when running migration

查看:78
本文介绍了运行迁移时如何解决外键错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不知道为什么它仍然不起作用并显示以下内容:

I don't know why it's still don't work and show this:

Illuminate \ Database \ QueryException:SQLSTATE [HY000]:常规 错误:1005无法创建表db_rocnikovka.books(错误号:150 外键约束格式不正确")(SQL:Alter表 books添加约束books_doba_foreign外键(doba) 引用druh_knihies(id_druhu))

Illuminate\Database\QueryException : SQLSTATE[HY000]: General error: 1005 Can't create table db_rocnikovka.books (errno: 150 "Foreign key constraint is incorrectly formed") (SQL: alter table books add constraint books_doba_foreign foreign key (doba) references druh_knihies (id_druhu))

在C:\ xampp \ htdocs \ Rocnikovka \ vendor \ laravel \ framework \ src \ Illuminate \ Database \ Connection.php:665

at C:\xampp\htdocs\Rocnikovka\vendor\laravel\framework\src\Illuminate\Database\Connection.php:665

异常跟踪: 1 PDOException::("SQLSTATE [HY000]:常规错误:1005无法创建>表db_rocnikovka.books(错误号:150"外键约束格式不正确)") C:\ xampp \ htdocs \ Rocnikovka \ vendor \ laravel \ framework \ src \ Illuminate \ Database \ Connection.php:459

Exception trace: 1 PDOException::("SQLSTATE[HY000]: General error: 1005 Can't create > table db_rocnikovka.books (errno: 150 "Foreign key constraint is incorrectly formed")") C:\xampp\htdocs\Rocnikovka\vendor\laravel\framework\src\Illuminate\Database\Connection.php:459

2 PDOStatement :: execute()

2 PDOStatement::execute()

  C:\xampp\htdocs\Rocnikovka\vendor\laravel\framework\src\Illuminate\Database\Connection.php:459

 catch (Exception $e) {
    throw new QueryException(
       $query, $this->prepareBindings($bindings), $e
    );
 }

第一张桌子

class CreateDruhKnihiesTable extends Migration
{
    public function up()
    {
        Schema::create('druh_knihies', function (Blueprint $table) {
            $table->bigIncrements('id_druhu');
            $table->string('nazev');
            $table->timestamps();
        });
    }

    public function down()
    {
        Schema::dropIfExists('druh_knihies');
    }
}

第二张表

class CreateBooksTable extends Migration
{
    public function up()
    {
        Schema::create('books', function (Blueprint $table) {
            $table->bigIncrements('id_book');
            $table->string('nazev');
            $table->string('autor');
            $table->string('druh');
            $table->unsignedInteger('doba');
            $table->foreign('doba')->references('id_druhu')->on('druh_knihies');
            $table->integer('pocet_stranek');
            $table->timestamps();
        });
    }

    public function down()
    {
        Schema::dropIfExists('books');
    }
}

推荐答案

您的外键必须具有与其引用的键相同的类型.因此,需要将第二个表外键doba更改为此,因为您在第一个表主键中使用了BigIncrements().

You foreign key needs to have the same type as the key it references. Therefor the second table foreign key doba needs to be change to this, becuase you use BigIncrements() in the first table primary key.

$table->unsignedBigInteger('doba');

这篇关于运行迁移时如何解决外键错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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