"php artisan migrate" shows "nothing to migrate"
Asked Answered
A

8

6

I am new to laravel. I am working on laravel version 6. I have created migration. It works nicely the first time, but if i change something in the migration file and then i run php artisan migrate it shows nothing to migrate. I tried php artisan migrate --path as well but it does not work. To make it work i have to delete the migration file and create it again. I don't want to use php artisan migrate:fresh.

what should i do to run only one migrations file which has been changed?

Arther answered 2/10, 2019 at 14:0 Comment(6)
try php artisan migrate:refreshBaxie
migrate:refresh will delete all table and then run migration again.Artifice
Please use php artisan migrate:rollback for last created migration file.Artifice
so php artisan migrate:rollback will not drop all the tables?Arther
All of this information about rollback and reset aside, if you want to change a migration that has already been run, the rule of thumb is "don't". Create a new migration that changes the table (add/drop/change column, etc) and run php artisan migrate again. Migrations are meant to be a "moving forward" type of thing so you don't have to run rollbacks and risk dataloss.Gratian
If you don't like to create a new migration file, make your own artisan command.Ashtray
A
4

When you run php artisan migrate it'll check migration table if there is no new file in the migration folder it'll show you nothing to migrate.

If you want to rollback last migration.

To rollback the latest migration operation, you may use the rollback command. This command rolls back the last "batch" of migrations, which may include multiple migration files:

php artisan migrate:rollback

It'll delete the last created table.

The migrate:reset command will roll back all of your application's migrations:

php artisan migrate:reset

The migrate:fresh command will drop all tables.

php artisan migrate:fresh

php artisan migrate:fresh --seed

more info : document

Artifice answered 2/10, 2019 at 14:7 Comment(5)
Is it migrate:fresh or migrate:refresh? Edit: Nevermind; I see it in the newer documentation: laravel.com/docs/6.x/migrationsGratian
migrate:refresh will delete all table and auto-create it again. migrate:fresh only delete table.Artifice
If you want to delete only last created table you must use migrate:rollbackArtifice
I don't mind but in new document migration concept is the same.laravel.com/docs/6.x/migrations#rolling-back-migrationsArtifice
Gotcha; I hadn't seen the fresh command before, but seems to be a newer addition (Laravel 5.5+) that effectively does the same thing as refresh, but doesn't rollback; it just drops everything and does it again. Good to know!Gratian
B
2

Sadly impossible. The best workaround is to use seeders and use php artisan db:seed after you use php artisan migrate:fresh. Why don't you want to use that?

Byrdie answered 2/10, 2019 at 14:5 Comment(3)
Because it will drop my users table as well. so everytime i make some changes to the migration file, i'll have to add user as well.Arther
@AkshayRathod for adding data seeders are inventedOverstuff
i am new to laravel. i don't know what seeders are, i have to look that up.Arther
F
2

there is two things to do you that you can use 1. In your database there is a table called migrations. Delete the rows from there which one is you want to migrate. That should be there. 2.create a folder inside of database/migrations/folder. And put the migrations file inside the folder then in your command prompt run this below command:

php artisan migrate:refresh --path=database/migrations/folder

option 2 is better than the option 1. I always use this. So i recommend option 2. This should be work

Fester answered 2/10, 2019 at 14:42 Comment(0)
C
2

For every modification on existing (already migrated tables) you will have to make a new migration with modifications only. If you have "users" table migration 2014_10_12_000000_create_users_table like:

 Schema::create('users', function (Blueprint $table) {
        $table->bigIncrements('id');
        $table->string('name');
        $table->string('email')->unique();
        $table->string('password');         
        $table->rememberToken();
        $table->timestamps();
    });

and you need to split "name" column, will have to php artisan make:migration alter_table_users --table="users" and add what you want to change:

Schema::table('users', function (Blueprint $table) {
    $table->renameColumn('name', 'first_name'); // rename column
    $table->string('last_name'); // add new column  });

Reverse

public function down()
    {
        Schema::table('users', function (Blueprint $table) {
            $table->renameColumn('first_name', 'name');
            $table->dropColumn('last_name');
        });
    }

Nou you can use php artisan migrate

Documentation: https://laravel.com/docs/6.x/migrations#modifying-columns

Cresting answered 22/1, 2020 at 13:38 Comment(0)
K
2

If you have seeder data then you simply do: php artisan migrate: fresh -- seed . It help to re-migrate your migration with seeder data

Keyte answered 22/1, 2020 at 16:49 Comment(0)
E
1

You didn't understand well migrations mechanics.

but if i change something in the migration file and then i run php artisan migrate it shows nothing to migrate

You write migration to make some changes in database and run it once. If you want to do next updates to database you need next migration.

During development process you can rerun migrations by php artisan migrate:fresh, but on production make sure your migration makes everything you want.

Laravel stores informations about migrations in database in table 'migrations'. If you want to reset some migrations files you can try deleting or edit some records in that table.

Etherege answered 2/10, 2019 at 14:22 Comment(1)
The migrations table stores the name (key) of the migration and the batch. There's not much point to manually editing that table when you have the various migration commands to do it for you. It also doesn't have an id or autoincrement column, so running SQL against it is sometimes a hassle.Gratian
D
1

Have you checked that the migration has already run or not in the migration table. If its run then there will be a row respective to your migration. If you do changes to an old migration than nothing will be reflected when you run command php artisan migrate, as it has already been migrated.

Drizzle answered 2/10, 2019 at 15:28 Comment(0)
D
1

No one replied question "Akshay Rathod" but "Militaru" replied exactly what he need php artisan make:migration alter_table_users --table="users" and copy you new fields inside the up() function as under

public function up()
    {
        Schema::table('users', function (Blueprint $table) {
            $table->string('userimage')->nullable();
            $table->string('contact_no')->nullable();
            $table->string('website')->nullable();
            $table->string('country_id')->nullable();
            $table->string('timezone')->nullable();
            $table->string('currency')->nullable();
            $table->string('communication_email')->default(1);
            $table->string('communication_phone')->default(0);
            $table->string('allow_marketing')->default(0);
        });
    }
Delate answered 19/12, 2021 at 6:8 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.