Laravel 5.5 error Base table or view already exists - BOOKC -->

Laravel 5.5 error Base table or view already exists

Sunday, December 10, 2017

Laravel 5.5 error Base table or view already exists

Yesterday when i'm going to create new migration for table called 'articles' i got an error message when i run the php artisan migrate command, the error is like this:

[Illuminate\Database\QueryException] SQLSTATE[42S01]: Base table or view already exists: 1050 Table 'articles' already exists (SQL: create table `articles` ( `article_id` int unsigned not null auto_increment primary key, `author_id` int not null, `title` varchar(255) not null, `content` text not null, `created_at` timestamp null, `updated_at` timestamp null) default character set utf8mb4 collate utf8mb4_unicode_ci)
[PDOException] SQLSTATE[42S01]: Base table or view already exists: 1050 Table 'articles' already exists

The solution for this problem is just to delete the table that's conflicted, in this case i have to delete table 'articles' and then run the migration again, this time should be no problem.

You can delete from phpmyadmin or workbench if you use that, you can also delete from artisan tinker, simply run these:
php artisan tinker
Schema::drop('articles')

Alternatively, you can also add command to drop table if exist in the up method of the migration class, like this:
<?php

use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class CreateTableArticles extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::dropIfExists('articles');
Schema::create('articles', function (Blueprint $table) {
$table->increments('article_id');
$table->integer('author_id');
$table->string('title');
$table->text('content');
$table->timestamps();
});
}

/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('articles');
}
}