php 在 Laravel 中安全地移除迁移
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16871413/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Safely remove migration In Laravel
提问by Globalz
In Laravel, there appears to be a command for creating a migration, but not removing.
在 Laravel 中,似乎有一个创建迁移的命令,但没有删除。
Create migration command:
创建迁移命令:
php artisan migrate:make create_users_table
If I want to delete the migration, can I just safely delete the corresponding migrations file within the database/migrations folder?
如果我想删除迁移,我可以安全地删除数据库/迁移文件夹中的相应迁移文件吗?
Migrations file:
迁移文件:
2013_05_31_220658_create_users_table
回答by malisokan
I accidentally created a migration with a bad name (command: php artisan migrate:make
). I did not run(php artisan migrate
) the migration, so I decided to remove it.
My steps:
我不小心创建了一个名称错误的迁移(命令:)php artisan migrate:make
。我没有运行( php artisan migrate
) 迁移,所以我决定删除它。我的步骤:
- Manually delete the migration file under
app/database/migrations/my_migration_file_name.php
- Reset the composer autoload files:
composer dump-autoload
- Relax
- 手动删除下的迁移文件
app/database/migrations/my_migration_file_name.php
- 重置 Composer 自动加载文件:
composer dump-autoload
- 放松
If you did run the migration (php artisan migrate
), you may do this:
如果您确实运行了迁移 ( php artisan migrate
),您可以这样做:
a)Run migrate:rollback
- it is the right way to undo the last migration (Thnx @Jakobud)
a)运行migrate:rollback
- 这是撤消上次迁移的正确方法(Thnx @Jakobud)
b)If migrate:rollback
does not work, do it manually (I remember bugs with migrate:rollback in previous versions):
b)如果migrate:rollback
不起作用,请手动执行(我记得以前版本中 migrate:rollback 的错误):
- Manually delete the migration file under
app/database/migrations/my_migration_file_name.php
- Reset the composer autoload files:
composer dump-autoload
- Modify your database: Remove the last entry from the migrationstable
- 手动删除下的迁移文件
app/database/migrations/my_migration_file_name.php
- 重置 Composer 自动加载文件:
composer dump-autoload
- 修改您的数据库:从迁移表中删除最后一个条目
回答by Jason Lewis
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.
如果迁移已运行(阅读:已迁移),那么您应该回滚迁移以清除数据库表中的历史记录。回滚后,您应该能够安全地删除迁移文件,然后再次继续迁移。
回答by joash
php artisan migrate:fresh
Should do the job, if you are in development and the desired outcome is to start all over.
应该完成这项工作,如果您处于开发阶段并且想要的结果是从头开始。
In production, that maybe not the desired thing, so you should be adverted. (The migrate:fresh command will drop all tables from the database and then execute the migrate command).
在生产中,这可能不是想要的东西,所以你应该被告知。( migrate:fresh 命令将从数据库中删除所有表,然后执行 migrate 命令)。
回答by Stephane
You likely need to delete the entry from the migrations table too.
您可能还需要从迁移表中删除该条目。
回答by JR Tan
I accidentally created two times create_users_table. It overrided some classes and turned rollback into ErrorException.
我不小心创建了两次create_users_table。它覆盖了一些类并将回滚变成了 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
您需要做的是在 vendor/composer 文件夹中找到 autoload_classmap.php 并查找特定的代码行,例如
'CreateUsersTable' => $baseDir . '/app/database/migrations/2013_07_04_014051_create_users_table.php',
and edit path. Then your rollback should be fine.
并编辑路径。那么你的回滚应该没问题。
回答by Udhav Sarvaiya
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 versionthat will allow you to back out a single migration:
Laravel 5.3 及以上版本添加了一项新功能,允许您取消单个迁移:
php artisan migrate:rollback --step=1
after, Manually delete the migration file under database/migrations/my_migration_file_name.php
之后,手动删除下的迁移文件 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
这样,你就可以安全的删除laravel中的迁移,只需要2步
回答by DAVID AJAYI
I will rather do it manually
我宁愿手动完成
- Delete the model first (if you don't) need the model any longer
- Delete the migration from
...database/migrations
folder - 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 - Still within your database, in the migrations folder, locate the row with that migration file name and delete the row.
- 先删除模型(如果你不需要)不再需要模型
- 从
...database/migrations
文件夹中删除迁移 - 如果您已经迁移,即如果您已经运行
php artisan migrate
,请登录您的 phpmyadmin 或 SQL(无论哪种情况)并在您的数据库中,删除迁移创建的表 - 仍在您的数据库中的迁移文件夹中,找到具有该迁移文件名的行并删除该行。
Works for me, hope it helps!
对我有用,希望能帮到你!
回答by ederrafo
This works for me:
这对我有用:
- I deleted all tables in my database, mainly the migrations table.
php artisan migrate:refresh
- 我删除了数据库中的所有表,主要是迁移表。
php artisan migrate:refresh
in laravel 5.5.43
在 Laravel 5.5.43