【问题标题】:When i try and migrate my tables in laravel i keep getting the following error当我尝试在 laravel 中迁移我的表时,我不断收到以下错误
【发布时间】:2021-03-29 05:27:55
【问题描述】:

我正在尝试运行 php artisan migrate 但我不断收到以下错误:

我试图回滚表并再次迁移它们,但它只是给了我同样的错误

SQLSTATE[HY000]: General error: 1215 Cannot add foreign key constraint (SQL: alter table `comments` add constraint `comments_post_id_foreign` foreign key (`post_id`) references `posts` (`id`) on delete cascade)

这是我的 create_cmets_table:

<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

class CreateCommentsTable extends Migration
{
/**
 * Run the migrations.
 *
 * @return void
 */
public function up()
{
    Schema::create('comments', function (Blueprint $table) {
        $table->id();
        $table->mediumText('body');
        $table->string('email');
        $table->string('name');
        $table->boolean('approved');
        $table->integer('post_id')->unsigned();
        $table->timestamps();

    });

    Schema::table('comments', function ($table) {
        $table->foreign('post_id')->references('id')->on('posts')->onDelete('cascade');
    });

}

/**
 * Reverse the migrations.
 *
 * @return void
 */
public function down()
{
    Schema::dropforeign(['post_id']);
    Schema::dropIfExists('comments');
}
}

这是我的 create_posts_table:

<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

class CreatePostsTable extends Migration
{
/**
 * Run the migrations.
 *
 * @return void
 */
public function up()
{
    Schema::create('posts', function (Blueprint $table) {
        $table->id();
        $table->string('title');
        $table->mediumText('body');
        $table->string('name');
        $table->timestamps();
    });
}

/**
 * Reverse the migrations.
 *
 * @return void
 */
public function down()
{
    Schema::dropIfExists('posts');
}
}

我的迁移表是按以下顺序创建的:

create users table
create passwords 
create failed jobs
create posts 
create comments

我该如何解决这个问题?

【问题讨论】:

标签: php laravel laravel-artisan artisan-migrate


【解决方案1】:
<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

class CreateCommentsTable extends Migration
{
/**
 * Run the migrations.
 *
 * @return void
 */
public function up()
{
    Schema::create('comments', function (Blueprint $table) {
        $table->id();
        $table->mediumText('body');
        $table->string('email');
        $table->string('name');
        $table->boolean('approved');
        $table->integer('post_id')->unsigned();
        $table->timestamps();
        $table->foreign('post_id')->references('id')->on('posts')->onDelete('cascade');
    });
}

/**
 * Reverse the migrations.
 *
 * @return void
 */
public function down()
{
    Schema::dropIfExists('comments');
}
}

尝试更新评论迁移文件这样

【讨论】:

  • 是的,对不起,我忘了在上面进行更改。即使我将其更改为帖子,它仍然会给我错误
  • 请尝试更新评论迁移文件,就像我提到的那样
  • 并确保posts表需要在cmets表之前创建
  • 当我运行 php artisan migrate 时,我认为只有我的 create_cmets_table 迁移,因为它从该表开始,如果我删除表并再次运行该命令,即使我回滚,它也不会迁移。这可能是问题吗?
  • 是的,这是问题所在,:D 但是如果你更新我上面提到的内容,并尝试运行php artisan migrate:fresh,我认为它应该可以工作
猜你喜欢
  • 1970-01-01
  • 2020-01-07
  • 2016-05-05
  • 1970-01-01
  • 2014-10-21
  • 2010-10-20
  • 2014-05-03
  • 1970-01-01
  • 2015-09-20
相关资源
最近更新 更多