Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/laravel/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在laravel中添加外键时出错_Laravel - Fatal编程技术网

在laravel中添加外键时出错

在laravel中添加外键时出错,laravel,Laravel,我是拉拉维尔的初学者。我在laravel中添加外键时遇到问题。我试过了,但找不到错误 [Illuminate\Database\QueryException] SQLSTATE[HY000]: General error: 1005 Can't create table laravel_home1 #sql-4b8_2a (errno: 150 "Foreign key constraint is incorrectly formed") (SQL: alter table `posts`

我是拉拉维尔的初学者。我在laravel中添加外键时遇到问题。我试过了,但找不到错误

[Illuminate\Database\QueryException]
SQLSTATE[HY000]: General error: 1005 Can't create table laravel_home1

#sql-4b8_2a (errno: 150 "Foreign key constraint is incorrectly formed")

(SQL: alter table `posts` add constraint
     `posts_comment_id_foreign` foreign key (`comment_id`) references
      `comments` (`id`))
表后迁移代码

public function up()
{
    Schema::create('posts', function (Blueprint $table) {
        $table->increments('id');
        $table->string('title');
        $table->string('content');       
        $table->integer('comment_id')->unsigned();
    });

    Schema::table('posts', function ($table) {
        $table->foreign('comment_id')
                ->references('id')
                ->on('comments');
    });
}
public function up()
{
    Schema::create('comments', function (Blueprint $table) {
        $table->increments('id');
        $table->string('comment');
        $table->timestamps();
    });
}


public function down()
{
    Schema::drop('comments');
}
注释表迁移代码

public function up()
{
    Schema::create('posts', function (Blueprint $table) {
        $table->increments('id');
        $table->string('title');
        $table->string('content');       
        $table->integer('comment_id')->unsigned();
    });

    Schema::table('posts', function ($table) {
        $table->foreign('comment_id')
                ->references('id')
                ->on('comments');
    });
}
public function up()
{
    Schema::create('comments', function (Blueprint $table) {
        $table->increments('id');
        $table->string('comment');
        $table->timestamps();
    });
}


public function down()
{
    Schema::drop('comments');
}

在laravel中添加外键的问题有三个原因:

  • 您已经多次执行了一些回滚或迁移了同一个迁移文件,这些文件保存在缓存中,因此导致了问题

  • 你忘了放
    ->unsigned()位于该索引的末尾

  • 具有主键的表(后来在别处用作外键)以前未迁移

  • 无论哪种方式,请尝试将
    ->unsigned()最后是这样的

    $table->integer('item_id')->unsigned();
    $table->foreign('item_id')->references('id')->on('items');
    
    然后运行
    composer dump autoload

    然后
    php artisan迁移

    这应该行得通

    更新 我发现您的迁移有什么问题:

    替换此项:

    public function up()
    {
        Schema::create('posts', function (Blueprint $table) {
            $table->increments('id');
            $table->string('title');
            $table->string('content');       
            $table->integer('comment_id')->unsigned();
        });
    
        Schema::table('posts', function ($table) {
            $table->foreign('comment_id')
                    ->references('id')
                    ->on('comments');
        });
    }
    
    为此:

    public function up()
    {
        Schema::create('posts', function (Blueprint $table) {
            $table->increments('id');
            $table->string('title');
            $table->string('content');       
            $table->integer('comment_id')->unsigned();
    
            $table->foreign('comment_id')
                ->references('id')
                ->on('comments');
        });
    
    }
    

    在laravel中添加外键的问题有三个原因:

  • 您已经多次执行了一些回滚或迁移了同一个迁移文件,这些文件保存在缓存中,因此导致了问题

  • 你忘了放
    ->unsigned()位于该索引的末尾

  • 具有主键的表(后来在别处用作外键)以前未迁移

  • 无论哪种方式,请尝试将
    ->unsigned()最后是这样的

    $table->integer('item_id')->unsigned();
    $table->foreign('item_id')->references('id')->on('items');
    
    然后运行
    composer dump autoload

    然后
    php artisan迁移

    这应该行得通

    更新 我发现您的迁移有什么问题:

    替换此项:

    public function up()
    {
        Schema::create('posts', function (Blueprint $table) {
            $table->increments('id');
            $table->string('title');
            $table->string('content');       
            $table->integer('comment_id')->unsigned();
        });
    
        Schema::table('posts', function ($table) {
            $table->foreign('comment_id')
                    ->references('id')
                    ->on('comments');
        });
    }
    
    为此:

    public function up()
    {
        Schema::create('posts', function (Blueprint $table) {
            $table->increments('id');
            $table->string('title');
            $table->string('content');       
            $table->integer('comment_id')->unsigned();
    
            $table->foreign('comment_id')
                ->references('id')
                ->on('comments');
        });
    
    }
    

    您已经迁移了注释表吗?如果
    注释
    表是在
    发布
    之前创建的,请同时共享
    注释
    表迁移。可能的重复项看起来像是您首先创建了表发布,和注释表不存在。还要检查字段是否为同一类型。您是否已经迁移了注释表?如果
    注释
    表是在
    发布
    之前创建的,请也共享
    注释
    表迁移。可能的重复项看起来像是您正在创建第一个表发布,和注释表不存在。还要检查字段是否为相同类型。