Laravel migrate - 一次性进行多个迁移(文件)

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

Laravel migrate - multiple migrations (files) in one go

phplaravelmigration

提问by Jeffz

Say, I have multiple migration files updating single table.

说,我有多个迁移文件更新单个表。

e.g.

例如

2016_03_20_072730_create_tasks_table.php
2016_03_20_075467_create_tasks_table.php

... which came from repo by different team members. Each is adjusting something in table, e.g. adding a column.

...来自不同团队成员的回购。每个都在调整表格中的某些内容,例如添加一列。

When I try to:

当我尝试:

php artisan migrate

I get error:

我得到错误:

PHP Fatal error:  Cannot declare class CreateTasksTable, because the name is
eady in use in U:\www\b10\database\migrations16_03_20_072737_create_tasks_
le.php on line 30


  [Symfony\Component\Debug\Exception\FatalErrorException]
  Cannot declare class CreateTasksTable, because the name is already in use

How should one deal with situation as described above?

应该如何处理上述情况?

EDIT

编辑

Here is the code:

这是代码:

2016_03_20_072730_create_tasks_table.php:

2016_03_20_072730_create_tasks_table.php:

class CreateTasksTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
       Schema::table('tasks', function ($table)
       {
           $table->string('task1');
       });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::drop('tasks');
    }
}

2016_03_20_075467_create_tasks_table.php:

2016_03_20_075467_create_tasks_table.php:

class CreateTasksTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
    Schema::table('tasks', function ($table)
        {
            $table->string('task2');
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::drop('tasks');
    }
}

采纳答案by Joel Hinz

Each migration has to have a unique class name. Rename the second one to something more sensible, such as 2016_03_20_075467_add_task2_to_tasks_tableand AddTask2ToTasksTable, then run composer dump-autoloadto make Laravel find the changes. Now you can migrate.

每个迁移都必须有一个唯一的类名。将第二个重命名为更合理的名称,例如2016_03_20_075467_add_task2_to_tasks_tableand AddTask2ToTasksTable,然后运行composer dump-autoload让 Laravel 找到更改。现在您可以迁移了。

Edit: Now that both migrations' code has been edited into the question, I can see that the first migration has the same problem, and should be renamed in the same manner. There's probably an initial create migration sometime further back. You should stop naming editation migrations anything with create, since you aren't actually creating the table.

编辑:既然两个迁移的代码都已编辑到问题中,我可以看到第一个迁移有相同的问题,应该以相同的方式重命名。可能在更早的某个时候进行初始创建迁移。您应该停止使用 来命名编辑迁移create,因为您实际上并没有创建表。