[Laravel + mysql] Launching a new application and getting stuck in the first php artisan migrate

Sep 6, 2020 PHP MySQL Laravel migrate

What kind of problem?

When I set up the development environment with laravel and mysql to create a Web application using the PHP language for the first time and migrate the newly created table, an error occurs and the table cannot be created normally.

#Error details When I typed the $ php artisan migrate command in the terminal, I got the following error: As shown in the 6th line, 1050 Table’users’ already exists … which means “the user table already exists”.

$ php artisan migrate
Migrating: 2014_10_12_000000_create_users_table

   Illuminate \ Database \ QueryException

  SQLSTATE [42S01]: Base table or view already exists: 1050 Table'users' already exists (SQL: create table `users` (` id` bigint unsigned not null auto_increment primary key, `name` varchar (255) not null,` email` varchar (255) not null, `email_verified_at` timestamp 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')

  at vendor / laravel / framework / src / Illuminate / Database / Connection.php: 671
    667 | // If an exception occurs when attempting to run a query, we'll format the error
    668 | // message to include the bindings with SQL, which will make this exception a
    669 | // lot more helpful to the developer instead of just the database's errors.
    670 | catch (Exception $ e) {
  > 671 | throw new QueryException (
    672 | $ query, $ this-> prepareBindings ($ bindings), $ e
    673 |);
    674 |}
    675 |

      +9 vendor frames
  10 database / migrations / 2014_10_12_000000_create_users_table.php: 24
      Illuminate \ Support \ Facades \ Facade :: __callStatic ("create")

      +22 vendor frames
  33 artisan: 37
      Illuminate \ Foundation \ Console \ Kernel :: handle (Object (Symfony \ Component \ Console \ Input \ ArgvInput), Object (Symfony \ Component \ Console \ Output \ ConsoleOutput))

Premise

macOS ver10.15.4 laravel ver4.0.0 installed mysql ver5.6.47 installed Connected to sequel pro

#Flow until error occurs On the terminal

$ laravel new [project name]

When you create a new project with the command, the user table is also automatically generated. (The password_resets table is also automatically generated.)

This time, I decided to make an additional item table,

$ php artisan make: migration create_items_table

Create a migration file for the item table with

$ php artisan migrate

When I executed, I got the error 1050 Table’users’ already exists … as mentioned above, and the item table could not be created.

Workaround

The user migration file has been modified as follows.

Change before

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

After change

<Upper row omitted>
class CreateUsersTable extends Migration
{
    / **
     * Run the migrations.
     *
     * @return void
     * /
    public function up ()
    {
        if (Schema :: hasTable ('users')) {
            // Return if the users table exists
            return;
        }
        Schema :: create ('users', function (Blueprint $ table) {
            $ table-> id ();
            $ table-> string ('name');
            $ table-> string ('email')-> unique ();
            $ table-> timestamp ('email_verified_at')-> nullable ();
            $ table-> string ('password');
            $ table-> rememberToken ();
            $ table-> timestamps ();
        });
    }
<Omitted below>

In the contents of public function up () in user’s migration file

if (Schema :: hasTable ('users')) {
            // Return if the users table exists
            return;

When I returned the existing table with the description of, migrate passed.

After resolving user, the same phenomenon occurred in the password_resets table, so we dealt with it in the same way.

Finally

Since I am a beginner about php, there are many things I do not understand yet, so I would appreciate it if you could point out any other best solution.