Laravel迁移-一口气进行多次迁移(文件) [英] Laravel migrate - multiple migrations (files) in one go

查看:58
本文介绍了Laravel迁移-一口气进行多次迁移(文件)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

说,我有多个迁移文件正在更新单个表.

Say, I have multiple migration files updating single table.

例如


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.

当我尝试:

php artisan migrate

我收到错误消息:


PHP Fatal error:  Cannot declare class CreateTasksTable, because the name is
eady in use in U:\www\b10\database\migrations\2016_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

如何处理上述情况?

编辑

这是代码:

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');
    }
}

推荐答案

每个迁移都必须具有唯一的类名.将第二个重新命名为更明智的名称,例如2016_03_20_075467_add_task2_to_tasks_tableAddTask2ToTasksTable,然后运行composer dump-autoload以使Laravel找到更改.现在您可以迁移了.

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_table and AddTask2ToTasksTable, then run composer dump-autoload to make Laravel find the changes. Now you can migrate.

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

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.

这篇关于Laravel迁移-一口气进行多次迁移(文件)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆