have 4 migrations as shown below. this is users table.
Schema::create('users', function (Blueprint $table) {
$table->increments('id');
$table->string('name');
$table->string('email')->unique();
$table->string('password');
$table->rememberToken();
$table->timestamps();
});
and this is artists migration.
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();
});
this is songs migration.
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();
});
}
and this is albums migration.
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();
});
and this is featuring which connects many to many , artists and songs.
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();
});
when i try to migrate these four migrations i get this error.
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`))