I am starting with a self-learning project built on top of Laravel 5.2 and I have found my first issue: self-reference in a migration.
This is how the file 2016_08_02_024942_create_navigation_table.php
looks like (I have remove the comments for not make the post too long):
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class CreateNavigationTable extends Migration
{
public function up()
{
Schema::create('navigation', function (Blueprint $table) {
$table->engine = 'InnoDB';
$table->increments('id')->unsigned();
$table->integer('position')->unsigned();
$table->string('title');
$table->string('slug');
$table->string('permissions')->nullable();
$table->timestamps();
$table->softDeletes();
});
}
public function down()
{
Schema::drop('articles');
}
}
And then because I have read several posts here like this, this, this and many more I make another file with just the relation named as 2016_08_02_030158_add_parent_to_navigation_table.php
with the following code:
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class AddParentToNavigationTable extends Migration
{
public function up()
{
Schema::table('navigation', function (Blueprint $table) {
$table->foreign('parent_id')->references('id')->on('navigation')->onUpdate('cascade')->onDelete('cascade');
});
}
public function down()
{
Schema::drop('articles');
}
}
But when I run the command php artisan migrate
I got the following error and I am not sure what I am doing wrong:
[Illuminate\Database\QueryException] SQLSTATE[42S01]: Base table or view already exists: 1050 Table 'navigation' already exists (SQL: create table
navigation
(id
int unsigned not null auto_increment primary key,position
int unsigned not null,title
varc har(255) not null,slug
varchar(255) not null,permissions
varchar(255) null,created_at
timestamp null,updated_at
timestamp null,deleted_at
timestamp null) default character set utf8 collate utf8_unicode_ci engin e = InnoDB)[PDOException] SQLSTATE[42S01]: Base table or view already exists: 1050 Table 'navigation' already exists
Can any give me some advice? What I am doing wrong? I have see this package but I am not sure if it will solve my issue.
SQLSTATE[42S01]: Base table or view already exists: 1050 Table 'navigation' already exists
Means you have same table name in database
So you need to verify, you don't have any table with that name in the database.
Run migration again
php artisan migration:rollback
Sometimes fails to drop table for different reasons.