PHP artisan migrate not creating new table

33,200

Solution 1

That means Laravel is trying to run the users table migration first. If you are in development and don't need to keep your data, you can just delete the users table and then run php artisan migrate

Solution 2

EDIT

move out already ran migrations from database\migrations folder, before running new migration files. after running the new migrations move back in the past migrations where it was before.

That means you have already ran php artisan migrate once and the table is already present in the database. sometimes you need to do a composer dump-autoload if the artisan is lying.

so you need to either rollback the last change before editing and running php artisan migrate . to rollback you could use php artisan migrate:rollback

Also if you want to remove all changes you could run php artisan migrate:reset .

There is php artisan migrate --force to force run the migration forcefully.

After doing all these steps and if not able to run migration, if its the development environment, drop the database create database again and run the php artisan migrate.

Solution 3

I have been having a similar problem with my Laravel setup (mac os Sierra 10.12.16) and using MAMP with atom and have not found a definitive solution until I followed the next steps.

When setting up up my local environment I have found using the following steps has prevented the migration issues later on,

IN AppServiceProvider.php  add the following code:

use Illuminate\Support\Facades\Schema;

public function boot()
{
    //
    Schema::defaultStringLength(191);
}

then in database.php:

'unix_socket' => '/Applications/MAMP/tmp/mysql/mysql.sock',

THEN FINALLY IN .env:

DB_CONNECTION=mysql
DB_HOST=localhost
DB_PORT=
DB_DATABASE=hackable
DB_USERNAME=root
DB_PASSWORD=root

Hope this helps someone as it took me days to finally get the setup correct :)

Solution 4

Your database already have this table, just drop it and let laravel create by artisan.

Solution 5

Do you have a users migration right? If so, insert your user create table inside of:

if(!Schema::hasTable('users')) ...
Share:
33,200
Prabin Parajuli
Author by

Prabin Parajuli

Updated on July 09, 2022

Comments

  • Prabin Parajuli
    Prabin Parajuli almost 2 years

    I am new to Laravel and I'm following Laravel Documentations as well as few Tutorial Videos. However, I am running this php artisan migrate code in my local CMD prompt and it's not creating Database Table in phpmyadmin. There are other few similar topics related to this in stackoverflow but none solved my issue. Please don't mark this duplicate.

    Ok, it goes like this. I run this code

    php artisan make:migration create_student_table --create=student
    

    and new file is created in migration folder as 2016_04_08_061507_create_student_table.php

    Then in that file I run this code

    use Illuminate\Database\Schema\Blueprint;
    use Illuminate\Database\Migrations\Migration;
    
        class CreateStudentTable extends Migration
        {
            /**
             * Run the migrations.
             *
             * @return void
             */
            public function up()
            {
                Schema::create('student', function (Blueprint $table) {
                    $table->increments('id');
                    $table->timestamps();
                    $table->string('name', 20); 
                    $table->string('email', 255);
                });
            }
    
            /**
             * Reverse the migrations.
             *
             * @return void
             */
            public function down()
            {
                Schema::drop('student');
            }
        }
    

    Then in cmd I run php artisan migrate but it didn't create student table. Instead it's showing this message

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

    I created users table few days ago using similar method as above. But new student table is not being created. Am I missing anything here? Thanks in advance.

  • Prabin Parajuli
    Prabin Parajuli about 8 years
    rather than deleting users table, i deleted users_migrations and it worked. any reason how did happen ? because in the near future, i may have to delete files in migrate folders manually to create new database table.
  • aimme
    aimme about 8 years
    its better to move out the already ran migration from database/migrations folder. remember to put it back after running the migration for the future use. @PrabinParajuli
  • Prabin Parajuli
    Prabin Parajuli about 8 years
    By MOVE OUT you mean create BACKUP to other folder or DELETE ? If I delete , won't I be needing these files ?
  • aimme
    aimme about 8 years
    you will be needing all these files for deployment.. so don't delete it..after running migration move it in to where it was.
  • Prabin Parajuli
    Prabin Parajuli about 8 years
    Thanx for the info.. worked for me.. Unfortunately I deleted previous migration file but I don't need that for now . Thanx again :)
  • Prabin Parajuli
    Prabin Parajuli about 8 years
    well that did too.. thnx
  • Graham
    Graham over 6 years
    Welcome to StackOverflow. Answers with only code tend to get flagged by the community as "low quality" and deleted. Please read the help section and see what makes a good Answer.