2
votes

Migration files in laravel is used to create the tables in the database, right? But when ever I try to migrate it gives me this error:

C:\xampp\htdocs\app>php artisan migrate

[Illuminate\Database\QueryException] SQLSTATE[42S01]: Base table or view already exists: 1050 Table 'users' already exists (SQL: create table users ( id int unsigned not null auto_increment primary key, name varchar(255) not null, email varchar(255) not null, password varchar(255) not null, remember_token varchar(100) null, created_at timestamp null, updated_at tim estamp null) default character set utf8mb4 collate utf8mb4_unicode_ci)

[PDOException] SQLSTATE[42S01]: Base table or view already exists: 1050 Table 'users' already exists


and I created a new migration file and it's called test. I know users already exist but I want to create the new table I created which is called test. I dropped all my tables and re-migrate it but it only created my users and migration table again. Not the new table I wanted to create which is test.

here is the migration file i am going to use to create my table but wont create:

    <?php

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

class CreateTestsTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('tests', function (Blueprint $table) {
            $table->increments('id');
            $table->timestamps();
        });
    }

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

here is the users migration file that tells me it exist:

<?php

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

class CreateUsersTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('users', function (Blueprint $table) {
            $table->increments('id');
            $table->string('name');
            $table->string('email')->unique();
            $table->string('password');
            $table->rememberToken();
            $table->timestamps();
        });
    }

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

here is the password migration file:

<?php

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

class CreatePasswordResetsTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('password_resets', function (Blueprint $table) {
            $table->string('email')->index();
            $table->string('token');
            $table->timestamp('created_at')->nullable();
        });
    }

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

here is the dummies migration file I also wanted to create but wont create for some reason even after i drop the all the tables:

<?php

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

class CreateDummiesTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('dummies', function (Blueprint $table) {
            $table->increments('id');
            $table->string('name');
            $table->text('body');
            $table->timestamp('date'); //if you dont put name for the timestamp it will create: create_at and update_at fields.
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('dummies');
    }
}
2
The only reason you would ever get that error is if your migration tries to create a table called users, when a table called users already exists. There is no other reason that error would be thrown. Drop the entire db, recreate it and run the migration again.Joe
could you paste all your migrations filenames?jakub wrona
@joe i dropped the entire db and re migrated again and it only created my users table and migrations table not my new tables that i wanted to create which is test and dummies. Why?Lami
@lami, paste the migration filenames, because your problem is that some of them are not executed.jakub wrona
have you tried running composer dump before running your migration?Alex Harris

2 Answers

0
votes

Are you using this command ? php artisan migrate:refresh

If you do then try this :

<?php

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

class CreateUsersTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::dropIfExists('users');
        Schema::create('users', function (Blueprint $table) {
            $table->increments('id');
            $table->string('name');
            $table->string('email')->unique();
            $table->string('password');
            $table->rememberToken();
            $table->timestamps();
        });
    }

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

And do the same thing in your others migrations files , that way, the table will be drop before every migration and you not have your problem anymore :) (it is quite the same thing as a php artisan migrate:refresh

0
votes

I think you should try this:

first remove tests, users, password_resets, dummies from migrations table in your database after then run php artisan migrate command