代码之家  ›  专栏  ›  技术社区  ›  Prafulla Kumar Sahu umang naik

1215无法添加外键

  •  0
  • Prafulla Kumar Sahu umang naik  · 技术社区  · 6 年前

    第5.6.3节 PHP 7.2.10版

    php artisan migrate:fresh

    General error: 1215 Cannot add foreign key constraint (SQL: alter table `videos` add constraint `videos_video_identified_by_foreign` foreign key (`video_identified_by`) references `users` (`id`))
    

    用户表混合文件-> 2014_10_12_000000_create_users_table 视频表迁移文件-> 2018_12_02_122553_create_videos_table

    通常情况下,当父表不存在,并且我们在表中使用它的列作为外键时会发生这种情况,但是可以看到,用户表应该先创建,然后创建videos表,那么为什么会出现这个错误。

    Schema::create('users', function (Blueprint $table) {
                $table->increments('id');
                $table->string('name');
                $table->string('email')->unique();
                $table->string('password');
                $table->rememberToken();
                $table->timestamps();
            });
    

     Schema::create('videos', function (Blueprint $table) {
        $table->increments('video_id');
        $table->text('video_link');
        $table->text('video_description');
        $table->string('video_category');
        $table->string('video_language');
        $table->unsignedInteger('video_identified_by');
        $table->timestamps();            
    });
    
    Schema::table('videos', function($table) {
        $table->foreign('video_identified_by')->references('id')->on('users');
    });
    
    3 回复  |  直到 6 年前
        1
  •  0
  •   haffis asma    6 年前

    嗨,试试这对我来说很好

    <?php
        $table->integer('video_identified_by')->unsigned();
        $table->foreign('video_identified_by')->references('id')->on('user')
              ->onUpdate('RESTRICT')->onDelete('CASCADE');      
    ?>
    

    然后是php artisan移植:刷新

        2
  •  0
  •   haffis asma    6 年前

    啊,你在桌上视频里改变你的身份试试这个

         Schema::create('videos', function (Blueprint $table) {
            $table->increments('id');
            $table->text('video_link');
            $table->text('video_description');
            $table->string('video_category');
            $table->string('video_language');
            $table->integer('video_identified_by')->unsigned();
            $table->foreign('video_identified_by')->references('id')->on('users')
              ->onUpdate('RESTRICT')->onDelete('CASCADE');   
            $table->timestamps();            
        });
    
        }
     public function down()
        {
            Schema::dropIfExists('videos');
        }
    
        3
  •  0
  •   Shikha    6 年前

    Schema::create('videos', function (Blueprint $table) {
    $table->increments('video_id');
    $table->text('video_link');
    $table->text('video_description');
    $table->string('video_category');
    $table->string('video_language');
    $table->unsigned('video_identified_by');
    $table->timestamps();            
    });
    
    Schema::table('videos', function($table) {
    $table->foreign('video_identified_by')->references('id')->on('users')- 
    >onDelete('cascade');
    });
    
    推荐文章