laravel 5.4 迁移 errno: 150“外键约束格式不正确”

Mah*_*med 2 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();
        });
Run Code Online (Sandbox Code Playgroud)

这就是艺术家的迁徙。

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();
        });
Run Code Online (Sandbox Code Playgroud)

这是歌曲迁移。

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();
        });
    }
Run Code Online (Sandbox Code Playgroud)

这是专辑迁移。

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();
        });
Run Code Online (Sandbox Code Playgroud)

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

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();
});
Run Code Online (Sandbox Code Playgroud)

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

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`))
Run Code Online (Sandbox Code Playgroud)

Sag*_*tam 5

songs您首先创建了 table ,然后创建了 table albums。当您尝试album_idsongs表中添加外键时,albums表尚未创建,因此您无法在不创建该表的情况下将外键添加到表中。

因此,您需要的是,在表albums之前创建表songs