简体   繁体   English

无法迁移外键

[英]Can't migrate foreign keys

UPDATE: I'm going to abandon the foreign key stuff for now.更新:我现在要放弃外键的东西。 Seems like my project is broken on a fundamental level so I'll revisit this project sometime later.似乎我的项目在基本层面上被打破了,所以我稍后会重新审视这个项目。 I appreciate the two helpful lads in the comments.我感谢评论中的两个乐于助人的小伙子。

I'm trying to encorporate foreign keys into my Laravel application but I just keep getting stuck.我正在尝试将外键合并到我的 Laravel 应用程序中,但我一直卡住。 I keep getting "errno: 150 "Foreign key constaint is incorrectly formed".我不断收到"errno: 150 "Foreign key constaint is incorrectly formed".

Even after trying multiple solutions, deleting the table, changing migration order etc, I keep getting back to this error.即使在尝试了多种解决方案、删除表、更改迁移顺序等之后,我仍然会遇到这个错误。 It's extremely frustrating.这非常令人沮丧。

I have also tried php artisan migrate:refresh and php artisan migrate:reset but nothing seems to help.我也尝试过php artisan migrate:refreshphp artisan migrate:reset但似乎没有任何帮助。

In the code below there is a piece of code from the CreateQuestionsTable class在下面的代码中有一段来自CreateQuestionsTable class的代码

        Schema::table('questions', function(Blueprint $table){
        $table->foreign('answerTypeName')->references('answerType')->on('answerstypes');
    });

I have tried to put this in the CreateAnswersTable class but that didn't solve it either.我试图把它放在CreateAnswersTable class中,但这也没有解决它。

In the code below there is another piece of code from the CreateQuestionsTable class在下面的代码中,还有来自CreateQuestionsTable class的另一段代码

        Schema::create('questions', function (Blueprint $table) {
        $table->increments('id');
        $table->string('questionName');
        $table->string('answerTypeName');
        $table->timestamps();
    });

Here I also tried to change answerTypeName to answerTypeId with $table->integer but that also didn't help.在这里,我还尝试使用$table->integeranswerTypeName更改为answerTypeId ,但这也没有帮助。

class CreateQuestionsTable extends Migration
{
 /**
 * Run the migrations.
 *
 * @return void
 */
public function up()
{
    Schema::create('questions', function (Blueprint $table) {
        $table->increments('id');
        $table->string('questionName');
        $table->string('answerTypeName');
        $table->timestamps();
    });

    Schema::table('questions', function(Blueprint $table){
        $table->foreign('answerTypeName')->references('answerType')->on('answerstypes');
    });
}
}

class CreateAnswertypesTable extends Migration
{
/**
 * Run the migrations.
 *
 * @return void
 */
public function up()
{
    Schema::create('answerstypes', function (Blueprint $table) {
        $table->bigIncrements('id');
        $table->string('answerType');
        $table->timestamps();
    });


}
}

This is an error that keeps popping up这是一个不断弹出的错误

PDOException::("SQLSTATE[HY000]: General error: 1005 Can't create table `impact_dockwize`.`questions` (errno: 150 "Foreign key constraint is incorrectly formed")")

I expect it to work like this but the error keeps popping up when I try to do php artisan migrate.我希望它能够像这样工作,但是当我尝试执行 php 工匠迁移时,错误不断弹出。 As I mentioned above, applying php artisan migrate:refresh or reset doesn't seem to work as well.正如我上面提到的,应用 php artisan migrate:refresh 或 reset 似乎也不起作用。

Look at this:看这个:

Schema::table('questions', function(Blueprint $table){
    $table->foreign('answerTypeName')->references('answerType')->on('answerstypes');
});

Remove it and leave it like this:删除它并像这样保留它:

Schema::create('questions', function (Blueprint $table) {
    $table->increments('id');
    $table->string('questionName');
    $table->string('answerTypeName');
    $table->timestamps();

    $table->foreign('answerTypeName')->references('answerType')->on('answerstypes');
});

Hope it helps!希望能帮助到你!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM