13

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
  • 551
  • 2
  • 12
  • 37
  • 1
    Migrations is like timeline for your project database. Laravel checks what migrations have been runned already and run only those whose are not runned yet. That timeline is saved to `migrations` table. So your problem here is that You do not have `migrations` table records, but have `users` table. I suggest You to drop all tables in database and then run `php artisan migrate` if You are just learning here. – Giedrius Kiršys Apr 08 '16 at 06:28
  • hmm thnx for the info, Ok deleting all tables will work for now. What if I persist the same problem in the future.? When I do the real project. what will be the solution there ? – Prabin Parajuli Apr 08 '16 at 06:55
  • You will not persist if You will not touch `migrations` table. Laravel will know what migrations have been already runned and will not to try to execute them again. – Giedrius Kiršys Apr 08 '16 at 06:58
  • using the flag :fresh will rewrite the table – Tgold brain Sep 12 '19 at 19:10

9 Answers9

14

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

Jeff
  • 24,623
  • 4
  • 69
  • 78
  • 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. – Prabin Parajuli Apr 08 '16 at 06:47
  • 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 – aimme Apr 08 '16 at 06:50
  • By **MOVE OUT** you mean create BACKUP to other folder or **DELETE** ? If I delete , won't I be needing these files ? – Prabin Parajuli Apr 08 '16 at 06:51
  • you will be needing all these files for deployment.. so don't delete it..after running migration move it in to where it was. – aimme Apr 08 '16 at 06:56
  • 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 Apr 08 '16 at 07:04
5

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.

aimme
  • 6,385
  • 7
  • 48
  • 65
2

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

1

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

Kayo Hamid
  • 31
  • 3
1

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

if(!Schema::hasTable('users')) ...
Kayo Hamid
  • 31
  • 3
1

why bother?

you can use php artisan migrate:fresh this will ignore the current user table and recreate the tables from fresh.

Tgold brain
  • 399
  • 5
  • 13
0

Sometimes if you are using MAMP server you might get this error because of database connectivity. This is because of the port number, usually while using MAMP the port number of mysql database is 8889 which you have to change in your configuration and run your artisan command it will work for sure.

0

When I faced this problem, I made a mistake in $table-> id(); but it should be $table->bigIncrements('id');

-2

This worked for me: delete all tables from database and run

php artisan migrate.
Stephen Rauch
  • 47,830
  • 31
  • 106
  • 135
  • 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. – Graham Feb 11 '18 at 03:08