Laravel 5 迁移:重命名列时出现错误

4

我是 Laravel 的新手,我有一个像这样的迁移:

public function up()
{
    Schema::table('mytable', function(Blueprint $table)
    {
        $table->renameColumn('mycol', 'old_mycol');
        $table->string('mycol', 100);
    });
}

当我运行它时,出现错误:

[PDOException]
SQLSTATE[42S21]: Column already exists: 1060 Duplicate column name 'mycol'

最终我将其拆分为两个单独的迁移,这样做很好,但我不明白为什么一次性完成会有问题。

1个回答

7

这是因为当执行迁移时,Laravel会在命令数组的最开始隐式放置任何添加新列或修改现有列的命令。以下代码直接取自Illuminate\Database\Schema\Blueprint类。

/**
 * Get the raw SQL statements for the blueprint.
 *
 * @param  \Illuminate\Database\Connection  $connection
 * @param  \Illuminate\Database\Schema\Grammars\Grammar  $grammar
 * @return array
 */
public function toSql(Connection $connection, Grammar $grammar)
{
    $this->addImpliedCommands();

    $statements = array();

    // Each type of command has a corresponding compiler function on the schema
    // grammar which is used to build the necessary SQL statements to build
    // the blueprint element, so we'll just call that compilers function.
    foreach ($this->commands as $command)
    {
        $method = 'compile'.ucfirst($command->name);

        if (method_exists($grammar, $method))
        {
            if ( ! is_null($sql = $grammar->$method($this, $command, $connection)))
            {
                $statements = array_merge($statements, (array) $sql);
            }
        }
    }

    return $statements;
}

/**
 * Add the commands that are implied by the blueprint.
 *
 * @return void
 */
protected function addImpliedCommands()
{
    if (count($this->getAddedColumns()) > 0 && ! $this->creating())
    {
        array_unshift($this->commands, $this->createCommand('add'));
    }

    if (count($this->getChangedColumns()) > 0 && ! $this->creating())
    {
        array_unshift($this->commands, $this->createCommand('change'));
    }

    $this->addFluentIndexes();
}

如上面的代码所示,在toSql方法中,会调用addImpliedCommands方法,在其中可能会向该对象的命令数组开头添加几个命令。这将导致重命名命令之前先执行新的mycol列的命令。
为了解决这个问题,您不需要创建两个迁移。在同一个迁移中,您可以简单地调用Schema::table()两次,如下所示:
Schema::table('mytable', function(Blueprint $table)
{
    $table->renameColumn('mycol', 'old_mycol');
});

Schema::table('mytable', function(Blueprint $table)
{
    $table->string('mycol', 100);
});

哇,非常感谢。这是我在这里的第一个问题,得到了如此明确的回答。我喜欢它!谢谢! - dorian108

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接