Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP artisan migrate not creating new table

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.

like image 558
Prabin Parajuli Avatar asked Apr 08 '16 06:04

Prabin Parajuli


3 Answers

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

like image 122
Jeff Avatar answered Oct 24 '22 10:10

Jeff


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.

like image 5
aimme Avatar answered Oct 24 '22 09:10

aimme


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 :)

like image 2
hiltondeville Avatar answered Oct 24 '22 10:10

hiltondeville