Safely remove migration In Laravel

前端 未结 9 1760
天涯浪人
天涯浪人 2021-01-29 17:46

In Laravel, there appears to be a command for creating a migration, but not removing.

Create migration command:

php artisan migrate:make create_users_tab         


        
相关标签:
9条回答
  • 2021-01-29 17:48

    DO NOT run php artisan migrate:fresh that's gonna drop all the tables

    0 讨论(0)
  • 2021-01-29 17:59

    You likely need to delete the entry from the migrations table too.

    0 讨论(0)
  • 2021-01-29 18:04

    I agree with the current answers, I just wanna add little more information.

    A new feature has been added to Laravel 5.3 and above version that will allow you to back out a single migration:

    php artisan migrate:rollback --step=1
    

    after, Manually delete the migration file under database/migrations/my_migration_file_name.php

    This is a great feature for when you run a migration

    In this way, you can safely remove the migration in laravel only in 2 step

    0 讨论(0)
  • 2021-01-29 18:05

    If the migration has been run (read: migrated) then you should roll back your migration to clear the history from your database table. Once you're rolled back you should be able to safely delete your migration file and then proceed with migrating again.

    0 讨论(0)
  • 2021-01-29 18:06

    I will rather do it manually

    1. Delete the model first (if you don't) need the model any longer
    2. Delete the migration from ...database/migrations folder
    3. If you have already migrated i.e if you have already run php artisan migrate, log into your phpmyadmin or SQL(whichever the case is) and in your database, delete the table created by the migration
    4. Still within your database, in the migrations folder, locate the row with that migration file name and delete the row.

    Works for me, hope it helps!

    0 讨论(0)
  • 2021-01-29 18:11

    I accidentally created two times create_users_table. It overrided some classes and turned rollback into ErrorException.

    What you need to do is find autoload_classmap.php in vendor/composer folder and look for the specific line of code such as

    'CreateUsersTable' => $baseDir . '/app/database/migrations/2013_07_04_014051_create_users_table.php',
    

    and edit path. Then your rollback should be fine.

    0 讨论(0)
提交回复
热议问题