PHP artisan migrate not creating new table
Asked Answered
C

10

14

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.

Chatterer answered 8/4, 2016 at 6:24 Comment(4)
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.Alf
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 ?Chatterer
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.Alf
using the flag :fresh will rewrite the tableSully
I
15

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

Inflation answered 8/4, 2016 at 6:27 Comment(5)
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.Chatterer
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. @PrabinParajuliChristine
By MOVE OUT you mean create BACKUP to other folder or DELETE ? If I delete , won't I be needing these files ?Chatterer
you will be needing all these files for deployment.. so don't delete it..after running migration move it in to where it was.Christine
Thanx for the info.. worked for me.. Unfortunately I deleted previous migration file but I don't need that for now . Thanx again :)Chatterer
C
6

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.

Christine answered 8/4, 2016 at 6:42 Comment(0)
T
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 :)

Tapestry answered 2/11, 2017 at 13:48 Comment(0)
T
1

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

Theologize answered 8/4, 2016 at 6:46 Comment(0)
T
1

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

if(!Schema::hasTable('users')) ...
Theologize answered 8/4, 2016 at 7:0 Comment(1)
well that did too.. thnxChatterer
S
1

why bother?

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

Sully answered 3/7, 2019 at 10:33 Comment(0)
A
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.

Ashby answered 18/1, 2019 at 11:33 Comment(0)
C
0

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

Concentre answered 31/10, 2021 at 18:12 Comment(0)
N
0

In my case, I had to execute the following commands :

$ php artisan migrate:reset # roll back all migrations
$ composer dump-autoload
$ php artisan migrate:fresh

The reason behind composer dump-autoload

Nailhead answered 13/12, 2023 at 17:36 Comment(0)
T
-2

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

php artisan migrate.
Tribute answered 11/2, 2018 at 2:29 Comment(1)
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.Edson

© 2022 - 2024 — McMap. All rights reserved.