【问题标题】:Yii2 migrate/down error on Windows XAMPPWindows XAMPP 上的 Yii2 迁移/关闭错误
【发布时间】:2018-06-15 02:33:35
【问题描述】:
  1. 在 Windows 上运行 XAMPP 机器,我尝试运行命令:
$ ./yii 迁移/向下 Yii 迁移工具(基于 Yii v2.0.15.1) 总共 1 个迁移被还原: m180614_020037_create_table_place_lang 恢复上述迁移? (是|否)[否]:是 *** 恢复 m180614_020037_create_table_place_lang > 从表 place_lang 中删除外键 fk_place_lang_id_place ... 完成(时间:0.067 秒) > 在 place_lang 上删除索引 idx_place_lang_id_place ... 完成(时间:0.189 秒) > drop table place_lang ...完成(时间:0.135s) *** 还原 m180614_020037_create_table_place_lang 失败(时间:0.396s) 1 个迁移中的 0 个已还原。 迁移失败。其余的迁移被取消。
  1. 我在下面运行了迁移

    使用 yii\db\Migration; // 类 m180614_020037_create_table_place_lang m180614_020037_create_table_place_lang 类扩展了迁移 { /** * {@inheritdoc} */ 公共函数 up() { $this->createTable('place_lang', [ 'id' => $this->primaryKey()->unsigned(), 'place_id' => $this->integer(11)->unsigned()->notNull(), 'locality' => $this->string(45)->notNull(), '国家' => $this->string(45)->notNull(), 'lang' => $this->string(2)->notNull() ]);
        $this->createIndex(
            'idx_place_lang_id_place',
            'place_lang',
            'place_id'
        );
    
        $this->addForeignKey(
            'fk_place_lang_id_place',
            'place_lang',
            'place_id',
            'place',
            'id'
        );
    
    
    }
    
    /**
     * {@inheritdoc}
     */
    public function down()
    {
        $this->dropForeignKey('fk_place_lang_id_place', 'place_lang');
        $this->dropIndex('idx_place_lang_id_place', 'place_lang');
        $this->dropTable('place_lang');
    
        return false;
    }
    
    }

    数据库表已删除,但迁移失败,我无法继续迁移堆栈。

在堆栈下面看到另一个迁移:

<?php

use yii\db\Migration;

/**
 * Class m180614_015652_create_table_place
 */
class m180614_015652_create_table_place extends Migration
{
    /**
     * {@inheritdoc}
     */
    public function up()
    {
        $this->createTable('place', [
            'id' => $this->primaryKey()->unsigned()->notNull(),
            'place_id' => $this->string(45)->notNull(),
            'lat' => $this->string(45)->notNull(),
            'lng' => $this->string(45)->notNull(),
            'country_code' => $this->string(2)->notNull(),
            'is_country' => $this->tinyInteger(4)->notNull()
        ]);
    }

    /**
     * {@inheritdoc}
     */
    public function down()
    {
        $this->dropTable('place');

        return false;
    }

}

这是 yii 2 在 Windows 和 XAMPP 上的限制,还是只是使用框架的新手错误?

【问题讨论】:

  • 另外,当我运行 ./yii migrate/fresh 时,一切都按预期工作
  • you returned false 在 down() 中,因此迁移假定它已失败。删除return false

标签: windows yii2


【解决方案1】:

在迁移中返回false 将其标记为失败,所以这就是原因。这是记录在案的:

并非所有迁移都是可逆的。例如,如果up() 方法删除了表中的一行,您可能无法在down() 方法中恢复该行。有时,您可能只是懒得实现down(),因为还原数据库迁移并不常见。在这种情况下,您应该在down() 方法中返回false,以表明迁移不可逆。

https://www.yiiframework.com/doc/guide/2.0/en/db-migrations

如果您不想控制迁移流程,您可能不应该返回任何东西。


./yii migrate/refresh 有效,因为它不使用down()。此命令从数据库中删除每个表并运行 migrate/up

【讨论】:

    猜你喜欢
    • 2012-07-03
    • 1970-01-01
    • 2011-02-14
    • 1970-01-01
    • 2021-03-18
    • 1970-01-01
    • 2018-04-24
    • 2021-04-08
    • 1970-01-01
    相关资源
    最近更新 更多