Php Laravel:迁移文件是否只在迁移和回滚之间运行一次?

Php Laravel:迁移文件是否只在迁移和回滚之间运行一次?,php,laravel,migration,Php,Laravel,Migration,在Laravel项目中,我在create table迁移文件中编写了以下命令: public function up() { Schema::create('employees', function (Blueprint $table) { $table->increments('id'); $table->timestamps(); }); } 并运行迁移命令: php artisan migrate 并且成功创建了表,然后我创建

在Laravel项目中,我在create table迁移文件中编写了以下命令:

public function up()
{
    Schema::create('employees', function (Blueprint $table) {
        $table->increments('id');
        $table->timestamps();
    });
}
并运行迁移命令:

php artisan migrate
并且成功创建了表,然后我创建了另一个迁移文件,用于修改我的表并添加一个新列,并编写了以下内容()

正如所料,该列已成功添加

我的问题是当我修改最后一个文件以添加新列时

public function up()
{
    Schema::table('employees', function (Blueprint $table) {
        $table->string('name');
        $table->string('address');
    });
}
和运行:

php artisan migrate
该命令没有给我任何要迁移的内容,因此我应该为每次修改创建一个新的迁移文件,还是运行其中一个命令

php artisan migrate:rollback


要我修改吗?但是在最后的命令中,数据会丢失,我不希望发生这种情况。

这个答案有两个部分

只要您还在开发中:是的,您可以随意编辑迁移并来回滚动,直到您对结果满意为止

一旦在其他地方(如生产系统)执行了迁移:您不应再更改该迁移。这里的要点是要有一个机制,可以重放结构中的更改。Laravel使用了一个非常简单的“我是否已经运行了此迁移?”——功能来查看它还必须为该数据库运行迁移的哪些部分。因此,将来将不再运行相同的迁移

TL;医生:谢谢你的问题

我应该为每次修改创建一个新的迁移文件吗


人们可以说:是的

正如ArSen所说,只要您仍在开发中,您就可以修改
迁移
文件,例如将它们合并到一个文件中

public function up()
{
    Schema::create('employees', function (Blueprint $table) {
        $table->increments('id');
        $table->string('name');
        $table->string('address');
        $table->timestamps();
    });
}
然后在终端中从


php-artisan-migrate:fresh

当您在本地开发时,通常会更新“迁移文件”。 每次更新后,您必须做第一件或第二件事:

run php artisan migrate:rollback --step=1 WITH OLD VERSION of migration file, after 
that change migration file as you wish and run "php artisan migrate" again


谢谢你兄弟的回答,如果这是我寻找最佳实践的唯一方法,我可以轻松地修改我的数据库创建多个修改文件。首先感谢你的回答:)如果我像你说的那样在本地开发,如果不担心丢失数据,我会使用第一选择,在第二种选择中,删除迁移文件是让laravel再次运行该文件的一个好主意,但是删除所有更改可能意味着删除一个表,正如您所说的,我认为如果其他表中有外键指向已删除表中的列,则可能会在其他表中出错,因此,例如,如果我重命名该表,则其他表将不会受此影响。
public function up()
{
    Schema::create('employees', function (Blueprint $table) {
        $table->increments('id');
        $table->string('name');
        $table->string('address');
        $table->timestamps();
    });
}
run php artisan migrate:rollback --step=1 WITH OLD VERSION of migration file, after 
that change migration file as you wish and run "php artisan migrate" again
manually delete record from migrations table, and manually delete all changes that 
migration produced (e.g. delete db table) and after that just run "php artisan migrate"