Laravel 5.4 具体表迁移

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/45473624/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 17:48:52  来源:igfitidea点击:

Laravel 5.4 Specific Table Migration

laravelmigrationlaravel-5.4

提问by Martney Acha

Hi read all the included documentation here in https://laravel.com/docs/5.4/migrations.

您好,请阅读https://laravel.com/docs/5.4/migrations 中包含的所有文档。

Is there a way on how to migrate a certain migration file (1 migration only), cause right now every time there is a change I use php artisan migrate:refreshand all fields are getting reset.

有没有办法迁移某个迁移文件(仅限 1 个迁移),因为现在每次我使用更改php artisan migrate:refresh并且所有字段都被重置。

回答by AddWeb Solution Pvt Ltd

First you should create one migrationfile for your table like:

首先,您应该migration为您的表创建一个文件,例如:

public function up()
    {
        Schema::create('test', function (Blueprint $table) {
            $table->increments('id');
            $table->string('fname',255);
            $table->string('lname',255);
            $table->rememberToken();
            $table->timestamps();
        });
    }

After create testfolder in migrationsfolder then newly created migrationmoved/copied in testfolder and run below command in your terminal/cmdlike:

迁移文件夹中创建测试文件夹后,新创建的迁移测试文件夹中移动/复制并在您的终端/cmd 中运行以下命令,如:

php artisan migrate --path=/database/migrations/test/

回答by Wissem SASSI

you should add the path to your migration file to refresh just this table and run

您应该将路径添加到迁移文件以仅刷新此表并运行

php artisan migrate:refresh --path=/database/migrations/fileName.php

回答by Sagar Gautam

Just look at the migrationstable in your database, there will be a list of migration file name and batch number value.

只需查看migrations数据库中的表,就会有一个迁移文件名和批号值的列表。

Suppose you have following structure,

假设你有以下结构,

id     migration                                           batch

1      2014_10_12_000000_create_users_table                  1 
2      2014_10_12_100000_create_password_resets_table        1
3      2016_09_07_103432_create_tabel_roles                  1

If you want to just rollback 2016_09_07_103432_create_tabel_rolesmigration, change it's migration batch value to 2 which is highest among all and then just execute following.

如果您只想回滚2016_09_07_103432_create_tabel_roles迁移,请将其迁移批处理值更改为最高的 2,然后执行以下操作。

php artisan migrate:rollback

Here only table with batch value 2 will be rolled back. Now, make changes to that table and run following console command.

这里只有批值为 2 的表将被回滚。现在,对该表进行更改并运行以下控制台命令。

php artisan migrate

Batch value in the migrationstable defines order of the migrations. when you rollback, migrations that are latest or have highest batch value are rolled back at first and then others. So, you can change the value in database and then rollback a particular migration file.

migrations表中的批处理值定义了迁移的顺序。回滚时,最新的或具有最高批值的迁移首先回滚,然后是其他迁移。因此,您可以更改数据库中的值,然后回滚特定的迁移文件。

Although it's not a good idea to change batch number every time because of relationship among the table structure, we can use this case for some cases where single table rollback doesn't violates the integrity among the tables.

虽然由于表结构之间的关系,每次更改批号不是一个好主意,但我们可以将这种情况用于某些单表回滚不破坏表之间完整性的情况。

Hope you understand.

希望你能理解。

回答by Ahmad Zahabi

You need to put the file(s) into a new directory (ex:selected) and then apply

您需要将文件放入新目录(例如:selected)然后应用

php artisan migrate  --path=/database/migrations/selected

if you need rollback:

如果您需要回滚:

php artisan migrate:rollback  --path=/database/migrations/selected

Note:

笔记:

php artisan migrate:refresh

this will rollback and then migrate all the migrations files in the default directory (/database/migrations)

这将回滚然后迁移默认目录 (/database/migrations) 中的所有迁移文件

回答by Dawam Raja

if you use tab for autocomplete

如果您使用选项卡进行自动完成

php artisan migrate --path='./database/migrations/2019_12_31_115457_create_coworking_personal_memberships_table.php'

回答by manh quan nguyen

php artisan help migrate

You will see the option:

您将看到以下选项:

--path[=PATH] The path to the migrations files to be executed

--path[=PATH] 要执行的迁移文件的路径

By the way, you can probably indicate the root folder of the file that you want to migrate:

顺便说一下,您可能可以指明要迁移的文件的根文件夹:

php artisan migrate --path=/database/migrations/sample.php

Or, You can create a new folder in migrations, then migrate all the migration files you want inside it:

或者,您可以在迁移中创建一个新文件夹,然后将您想要的所有迁移文件迁移到其中:

php artisan migrate --path=/database/migrations/new_folder

回答by tylik

Just wanted to post another solution, which i think is worth mentioning.

只是想发布另一个解决方案,我认为值得一提。

  1. Find row with your migration name in migrations table and DELETE it. It should look like this: 2016_06_01_000001_create_oauth_auth_codes_table
  2. Remove your table from database e.g. DROP TABLE oauth_auth_codes
  3. Run php artisan migrate
  1. 在迁移表中查找具有迁移名称的行并删除它。它应该是这样的:2016_06_01_000001_create_oauth_auth_codes_table
  2. 从数据库中删除您的表,例如 DROP TABLE oauth_auth_codes
  3. 运行 php artisan migrate

It will migrate only the table you need, and won't touch anything else

它只会迁移您需要的表,不会触及任何其他内容

回答by TCruz

Correction- remove slash before the database

更正-删除数据库前的斜杠

$ php artisan migrate --path=database/migrations/migration.php

回答by Jed

You can only rollback:

您只能回滚:

php artisan migrate:rollback

https://laravel.com/docs/5.4/migrations#rolling-back-migrations

https://laravel.com/docs/5.4/migrations#rolling-back-migrations

You can specify how many migrations to roll back to using the 'step' option:

您可以使用 'step' 选项指定要回滚的迁移数量:

php artisan migrate:rollback --step=1

Some tricks are available here:

这里有一些技巧:

Rollback one specific migration in Laravel

在 Laravel 中回滚一项特定的迁移

回答by sskoko

Delete the table and remove its record from migration table.

删除表并从迁移表中删除其记录。

After that you just run migration again:

之后,您只需再次运行迁移:

php artisan migrate