gpt4 book ai didi

php - 无法迁移外键

转载 作者:行者123 更新时间:2023-11-29 15:34:02 26 4
gpt4 key购买 nike

更新:我现在要放弃外键的东西。看来我的项目在根本上已经被破坏了,所以我稍后会重新审视这个项目。我很感谢评论中的两位乐于助人的小伙子。

我正在尝试将外键合并到我的 Laravel 应用程序中,但我总是陷入困境。我不断收到“errno:150“外键约束格式不正确”。

即使在尝试了多种解决方案、删除表、更改迁移顺序等之后,我仍然会遇到此错误。这非常令人沮丧。

我也尝试过 php artisan migrate:refreshphp artisan migrate:reset 但似乎没有任何帮助。

在下面的代码中,有一段来自 CreateQuestionsTable 类的代码

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

我尝试将其放入 CreateAnswersTable 类 中,但这也没有解决问题。

下面的代码中有来自 CreateQuestionsTable 类的另一段代码

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

在这里,我还尝试使用 $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();
});


}
}

这是一个不断弹出的错误

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

我希望它能像这样工作,但是当我尝试执行 php artisan migrate 时,错误不断弹出。正如我上面提到的,应用 php artisan migrate:refresh 或重置似乎效果不佳。

最佳答案

看看这个:

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

将其删除并保留如下:

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

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

希望对你有帮助!

关于php - 无法迁移外键,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58434567/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com