【问题标题】:laravel migration error while adding 2 foreign key添加2个外键时laravel迁移错误
【发布时间】:2018-08-01 21:50:12
【问题描述】:

我正在使用 Laravel 迁移来创建我的 MySQL 数据库,并且有两个表格论文和答案,并且需要使用外键连接两个表格。我有paper_idquestion_no 作为外键。但是在添加外键时出现错误。 我的纸桌和答题桌迁移

Schema::create('exampapers', function (Blueprint $table) {
        $table->engine = 'InnoDB';
        $table->increments('id');
        $table->integer('paper_id');
        $table->integer('question_no');
        $table->text('question');
        $table->string('answer1');
        $table->string('answer2');
        $table->string('answer3');
        $table->string('answer4');
        $table->integer('answerC');
        $table->string('knowarea');
        $table->timestamps();
        $table->index(['paper_id','question_no']);

});

Schema::create('answers', function (Blueprint $table) {
        $table->engine = 'InnoDB';
        $table->increments('id');
        $table->integer('paper')->unsigned();
        $table->integer('question')->unsigned();
        $table->integer('answers');
        $table->timestamps();
});

这是我创建外键的代码,

Schema::table('answers',function($table){
        $table->foreign('paper')->references('paper_id')->on('exampapers');
        $table->foreign('question')->references('question_no')->on('exampapers');
});

我通过 php artisan 得到的错误是,

Illuminate\Database\QueryException : SQLSTATE[HY000]: 一般错误: 1005 无法创建表exam_paper.#sql-b88_630 (errno: 150 "外键 约束格式不正确”)(SQL:alter table answers add 约束answers_paper_foreign 外键 (paper) 引用 exampapers (paper_id))

我参考了大多数其他帖子,并且已经尝试过unsignedInteger() 数据类型,在创建外键之前运行表创建。

我在代码中做错了什么?

【问题讨论】:

  • 你是先创建exampapers表再创建answers吗?另外,在answers 表中添加两个外键。如果你不明白,请告诉我!
  • @HirenGohel 我照你说的做了还是错误,Illuminate\Database\QueryException : SQLSTATE[HY000]: 一般错误:1005 无法创建表exam_paper.#sql-b88_63d (errno: 150 "外键约束格式不正确”)(SQL:更改表answers添加约束answers_paper_foreign外键(paper)引用exampaperspaper_id))
  • 添加$table->integer('paper_id')->unsigned()->nullable()->index();$table->text('question')->unsigned()->nullable()->index();
  • @HirenGohel 感谢它在添加 ->nullable()->index() 后有效
  • 很高兴能帮到你!!我已经发布了我的答案!

标签: mysql laravel foreign-keys migration


【解决方案1】:

您还需要将exampapers 表中的密钥设为无符号:

$table->integer('paper_id')->unsigned();

或者:

$table->insignedInteger('paper_id');

或者从外键定义中去掉unsigned()方法:

$table->integer('paper');

【讨论】:

  • 如果它未签名,我无法将数据正确添加到其中,那么如果我无法用数据填充试卷表,这将有什么帮助。请您详细解释一下。
  • @NaveedSheriffdeen unsigned 只是意味着您将无法添加像-20 这样的负整数。
  • 我为paper_idquestion_no 添加了unsigned(),但仍然出现错误 Illuminate\Database\QueryException : SQLSTATE[HY000]: General error: 1005 Can't create table exam_paper#sql-b88_647 (errno: 150 "Foreign key constraint is wrongly forms") (SQL: alter table answer add constraint answer_question_foreign foreign key (question) references exampapers (question_no))
  • @NaveedSheriffdeen 这与您未显示的其他表中的另一个外键无关。
  • 有一个错字。不是insigned,是unsigned。但是 SO 不会让我编辑,因为“编辑必须至少有 6 个字符”...
【解决方案2】:

您需要在两列中添加->unsigned()->nullable()->index();(即在paper_idquestion 中的exampapers 表中)。

尝试在exampapers表中添加如下:

$table->integer('paper_id')->unsigned()->nullable()->index();
$table->integer('question_no')->unsigned()->nullable()->index();

现在运行php artisan migrate 并解决问题!

希望对您有所帮助!

【讨论】:

  • @Naveed Sheriffdeen 你为什么选择这个答案? 1. 在 cmets 中,您显示了另一个表的下一个错误,这意味着我的解决方案显然适用于您的原始问题。 2. text('question') 与您的 FK 约束无关,因为您引用的是 question_no 而不是 question。 3.nullable()index()与FK约束完全无关。
  • @AlexeyMezenin:我已经用question_no 列更新了我的答案,这是 FK。此外,您的第三点是正确的,OP 只需要->unsigned()。但是作为OP解决的问题,他选择了我的答案!希望你能理解!如果OP也选择您的答案,我没有任何问题! :)
  • @Naveed 和@Alexey:据我了解,使用index():维护索引的数据库服务器的性能会受到很小的影响(当您将记录写入db) - 所以你通常只在需要的地方使用它们。
【解决方案3】:

我对您的迁移代码进行了一些修改。希望这会奏效

Schema::create('exampapers', function (Blueprint $table) {
        $table->engine = 'InnoDB';
        $table->increments('id');
        $table->integer('paper_id')->unsigned()->index();
        $table->integer('question_no')->unsigned()->index();
        $table->text('question');
        $table->string('answer1');
        $table->string('answer2');
        $table->string('answer3');
        $table->string('answer4');
        $table->integer('answerC');
        $table->string('knowarea');
        $table->timestamps();
        //$table->index(['paper_id','question_no']);

    });

Schema::create('answers', function (Blueprint $table) {
        $table->engine = 'InnoDB';
        $table->increments('id');
        $table->integer('paper')->unsigned()->index();
        $table->integer('question')->unsigned()->index();
        $table->integer('answers');
        $table->timestamps();
    });


Schema::table('answers',function($table){
        $table->foreign('paper')->references('paper_id')->on('exampapers');
        $table->foreign('question')->references('question_no')->on('exampapers');
    });

【讨论】:

    猜你喜欢
    • 2019-02-09
    • 2019-10-19
    • 2015-03-29
    • 2020-01-20
    • 2019-01-31
    • 2014-09-27
    • 1970-01-01
    • 2018-09-22
    • 2016-07-06
    相关资源
    最近更新 更多