I have migrations as follows:
create_users_table.php
class CreateUsersTable extends Migration {
public function up()
{
Schema::create('users', function(Blueprint $table) {
$table->increments('id');
$table->string('name', 255);
$table->string('username', 64)->unique();
$table->string('email', 255)->unique();
$table->string('password',64);
$table->timestamps();
});
}
public function down()
{
Schema::drop('users');
}
}
create_predictions_table.php
class CreatePredictionsTable extends Migration {
public function up()
{
Schema::create('predictions', function(Blueprint $table) {
$table->increments('id');
$table->integer('user_id')->unsigned();
$table->foreign('user_id')->references('id')->on('users');
$table->integer('fixture_id')->unsigned();
$table->foreign('fixture_id')->references('id')->on('fixtures');
$table->integer('home_team_score_prediction');
$table->integer('away_team_score_prediction');
$table->timestamps();
});
}
public function down()
{
Schema::drop('predictions');
}
}
create_fixtures_table.php
class CreateFixturesTable extends Migration {
public function up()
{
Schema::create('fixtures', function(Blueprint $table) {
$table->increments('id');
$table->integer('home_team_id');
$table->integer('away_team_id');
$table->dateTime('date');
$table->string('venue');
$table->timestamps();
});
}
public function down()
{
Schema::drop('fixtures');
}
}
Initially when I ran the migration using
php artisan migrate
I hadn't added foreign key's. I ran
php artisan migrate:refresh
after adding the foreign key constraints which gave me following errror:
SQLSTATE[42S01]: Base table or view already exists: 1050 Table 'predictions' already exists (SQL: create table `predictions` (`id` int unsigned not null auto_increment primary key, `user_id` int unsigned not null, `fixture_id` int
unsigned not null, `home_team_score_prediction` int not null, `away_team_score_prediction` int not null, `created_ at` timestamp default 0 not null, `updated_at` timestamp default 0 not null) default character set utf8 collate utf8_unicode_ci)
I think that is because the table was already there (although refresh is supposed to rollback the migrations and run them again)
When I tried to rollback using
php artisan migrate:rollback
I got the following:
[Illuminate\Database\QueryException]
SQLSTATE[23000]: Integrity constraint violation: 1217 Cannot delete or update a parent row: a foreign key constrain t fails (SQL: drop table `users`)
Where am I going wrong? I'm new to migrations, so these might be errors beginners usually get. But I don't understand what exactly I'm doing wrong. I hope someone can help me out here.
What I Did There is a migrations
table for Laravel. Truncate
or empty the table, then delete all tables related to your migrations. If possible remove any constraints to the foreign keys first before deleting the table.
You should be good to go migrating again.
Addition
Take care of the order of deletion of tables in down()
function.
Deleting some tables with referential constraints as declared by onDelete()
definitely needs constraints to be removed first.
I think that's causing your rollback error.
Look more into your migrations , buddy :-)
migration will continue until it meets some problem. So probably a foreign key has already been added, but its associated table is not created yet due to error. If you are trying to rollback, migration will assume that the table associated with a foreign key exists and it will try to rollback that table too.
However, that table does not exist!
Well, the simplest way is to empty the database and rerun migration. I met this problem before and find it hard to rollback step by step once such a problem occurs.