【问题标题】:Laravel & PostGres - Migration down() will not drop table (TimeScaleDB Extension)Laravel & PostGres - 向下迁移()不会删除表(TimeScaleDB 扩展)
【发布时间】:2021-02-13 00:54:07
【问题描述】:

这是create_facts_table 的样子:

class CreateFactsTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::connection('pgsql')->create('facts', function (Blueprint $table) {
            $table->bigInteger('entry_id');
            $table->string('tag');
            $table->timestampTz('time');
            $table->double('sensor_value');

            $table->index(['entry_id', 'tag', 'time']);
            $table->unique(['entry_id', 'tag', 'time']);
        });

        DB::connection('pgsql')->statement('SELECT create_hypertable(\'facts\', \'time\');');
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        // Tried both, neither would delete the table
        Schema::connection('pgsql')->dropIfExists('facts');
        //DB::connection('pgsql')->statement('DROP TABLE facts;');
    }
}

我没有收到来自down() 的任何错误。我能够以.env 文件中指定的数据库用户身份登录并运行DROP TABLE facts

当我运行php artisan migration:fresh up() 无法创建表并抛出重复表错误:

Duplicate table: 7 ERROR:  relation "facts" already exists

手动删除表后,我可以运行php artisan migration:fresh。我必须指定connection('pgsql'),因为我正在使用多个数据库。半无关,但我正在使用 TimeScaleDB 扩展(因此create_hypertable()

【问题讨论】:

    标签: laravel


    【解决方案1】:

    我最近对此进行了更深入的研究。我没有直接提到我正在使用 TimeScaleDB 扩展 ('SELECT create_hypertable(\'facts\', \'time\');')。

    我更新了问题的标题,以防将来有人从 Google 登陆。

    这是我学到的: 运行php artisan migration:fresh 时,它会尝试批量删除所有表,并且不使用down() 方法。 TimeScaleDB 超表不能批量删除。

    解决方案是改用php artisan migration:refresh,它将为每个表运行定义的drop() 操作。

    来源:https://stackoverflow.com/a/69105447/5449796

    【讨论】:

      猜你喜欢
      • 2022-07-07
      • 2014-02-03
      • 2015-08-05
      • 2016-09-11
      • 2020-12-01
      • 1970-01-01
      • 2018-01-30
      • 2021-08-11
      • 2021-03-31
      相关资源
      最近更新 更多