laravel 5.4 migration errno:150"外键约束形成错误"

时间:2017-06-11 04:08:47

标签: laravel laravel-5.4

有4次迁移,如下所示。 这是用户表。

 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('artists', function (Blueprint $table) {
            $table->increments('id');
            $table->string('name');
            $table->string('slug');
            $table->string('image')->nullable();
            $table->text('biography')->nullable();
            $table->integer('week_hits');
            $table->timestamp('week_date');
            $table->timestamp('viewed_now');
            $table->boolean('status')->default(1);
            $table->integer('user_id')->unsigned();
            $table->foreign('user_id')->references('id')->on('users');
            $table->timestamps();
        });

这是歌曲迁移。

Schema::create('songs', function (Blueprint $table) {
            $table->increments('id');
            $table->string('title');
            $table->text('lyrics')->nullable();
            $table->string('mp3');
            $table->string('youtube_id')->nullable();
            $table->timestamp('week_date');
            $table->integer('week_hits')->nullable();
            $table->timestamp('played_now')->nullable();
            $table->timestamp('hits');
            $table->integer('album_id')->unsigned()->nullable();
            $table->foreign('album_id')->references('id')->on('albums');
            $table->integer('artist_id')->unsigned();
            $table->foreign('artist_id')->references('id')->on('artists');
            $table->timestamps();
        });
    }

这是专辑迁移。

Schema::create('albums', function (Blueprint $table) {
            $table->increments('id');
            $table->string('name');
            $table->string('cover');
            $table->integer('artist_id')->unsigned();
            $table->foreign('artist_id')->references('id')->on('artists');
            $table->boolean('status')->default(true);
            $table->timestamp('viewed_now')->nullable();
            $table->integer('week_hits')->nullable();
            $table->timestamp('week_date');
            $table->timestamps();
        });

这是连接多对多,艺术家和歌曲的特色。

Schema::create('featuring', function (Blueprint $table) {
    $table->integer('artist_id')->unsigned()->nullable();
    $table->foreign('artist_id')->references('id')->on('artists');
    $table->integer('song_id')->unsigned()->nullable();
    $table->foreign('song_id')->references('id')->on('songs')->onDelete('cascade');
    $table->timestamps();
});

当我尝试迁移这四次迁移时,我收到此错误。

SQLSTATE[HY000]: General error: 1005 Can't create table `laravel`.`#sql-f0_11e` (errno: 150 "Foreign key constraint is incorrectly formed") (SQL: alter table `songs` add const
  raint `songs_album_id_foreign` foreign key (`album_id`) references `albums` (`id`))

2 个答案:

答案 0 :(得分:3)

您首先创建了表songs,然后创建了表albums。当您尝试在album_id表中添加外键songs时,尚未创建albums表,以便您无法在不创建表的情况下将外键添加到表中那张桌子。

那么,你需要的是,在albums表之前创建songs表。

答案 1 :(得分:1)

您可能无法在创建过程中设置外键。您必须创建表然后设置外键。您可以使用Schema :: create创建表,然后使用Schema :: table来设置外键。例如:

Schema::create('users', function (Blueprint $table) {
    $table->increments('id');
});

Schema::table('posts', function (Blueprint $table) {
    $table->integer('user_id')->unsigned();

    $table->foreign('user_id')->references('id')->on('users');
});