2
votes

Tying to create a new table in laravel schema and command php artisan make:migration create_asks_table --create tasks and having the schema like

<?php
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class CreateTasksTable extends Migration
{

    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {

        Schema::create('tasks', function (Blueprint $table) {
            $table->increments('id');
            $table->string('body');
            $table->timestamps();
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {

        Schema::dropIfExists('tasks');
    }
} ?>

and I am trying to create table using php artisan migrate command but it is not considering new table tasks to create it tries to create first table user that is already existing table that has been created in default and the error is

>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` timestamp null) default character set utf8mb4 collate utf8mb4_unicode_ci)

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

How can should I skip already existing table to create:

enter image description here

1

1 Answers

0
votes

Did you already create a users table before running migrations for the first time? Laravel comes with a migration that creates the users and passwords table in the format that Laravel expects.

That error means that the database you are migrating against already has the table users, but you have a migration file to create the users table in the migrations folder.

I reproduced the issue so I can help you understand it and walk through it with me:

  1. I ran php artisan migrate and get this error:

    [Illuminate\Database\QueryException]                                                                    
    SQLSTATE[42S01]: Base table or view already exists: 1050 Table 'users' 
    already exists (SQL: create table `us...  
    
  2. I checked my database and confirmed the users table exists.

  3. I ran this query to check the migrations already created by laravel:

    select * from migrations;
    
  4. I confirmed that the users migration is in my database and it's file is named this: the Users migration in my database
    Looking at my Migrations folder I see that the file name is different (could be because I imported an old laravel schema for example): enter image description here

In this case, all I have to do is rename the users file to this migration name.

nfml-5YF:admin$ php artisan migrate
Nothing to migrate.

If you created your own custom Users table and don't want laravels precanned users table If you created your own users table, then remove the users migration from the migrations folder. I typically create a pre-migrated folder in my migrations folder and drag the migrations I don't want in there.

I will warn you though that if you created your own users table, but still want to use Laravel's authentication workflow.... that you have to make sure it has all the fields laravel requires with the same field names, like a placeholder for the encrypted password, the login timestamps it requires, and the password hash to confirm passwords and usernames.