更新:我现在暂时放弃外键。看来我的项目在基本层面上是被破坏的,所以稍后我会重新访问该项目。我感谢两位乐于助人的小伙子。
我正在尝试将外键合并到我的Laravel应用程序中,但我一直陷入困境。我不断收到"errno: 150 "Foreign key constaint is incorrectly formed".
即使在尝试了多种解决方案,删除表,更改迁移顺序等之后,我仍然会回到这个错误。非常令人沮丧。
我也尝试过php artisan migrate:refresh
和php artisan migrate:reset
,但似乎无济于事。
下面的代码中有CreateQuestionsTable class
Schema::table('questions', function(Blueprint $table){
$table->foreign('answerTypeName')->references('answerType')->on('answerstypes');
});
我试图将其放在CreateAnswersTable class
中,但这也没有解决。
下面的代码中还有CreateQuestionsTable class
Schema::create('questions', function (Blueprint $table) {
$table->increments('id');
$table->string('questionName');
$table->string('answerTypeName');
$table->timestamps();
});
在这里,我还尝试使用answerTypeName
将answerTypeId
更改为$table->integer
,但这也无济于事。
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迁移时,错误不断弹出。如上所述,应用php artisan migrate:refresh或reset似乎不起作用。
答案 0 :(得分:0)
看看这个:
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');
});
希望有帮助!