【问题标题】:Laravel 8 - Change existing migrationLaravel 8 - 更改现有迁移
【发布时间】:2021-07-01 10:31:24
【问题描述】:

我正在Laravel Framework 8.33.1 上进行开发,并在我的本地环境和生产环境中进行了以下迁移。

class CreateCompanyTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('company', function (Blueprint $table) {
            $table->id();
            $table->integer('company_id');
            $table->integer('messageId');
            $table->string('url');
            $table->timestamps();

            /**
            New table:
            $table->id();
            $table->integer('company_id')->nullable($value = true);
            $table->integer('messageId');
            $table->integer('people_id')->nullable($value = true);
            $table->string('url')->nullable($value = true);
            $table->timestamps();
             */
        });
    }

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

我想使用以下适应字段更改迁移:

            $table->id();
            $table->integer('company_id')->nullable($value = true);
            $table->integer('messageId');
            $table->integer('people_id')->nullable($value = true);
            $table->string('url')->nullable($value = true);
            $table->timestamps();

由于我在生产中使用当前迁移,我不想丢失数据。

我只是尝试使用我的新表定义修改迁移文件,但我得到:

> php artisan migrate
Nothing to migrate.

关于如何在 laravel 中正确更改迁移有什么建议吗?

感谢您的回复!

【问题讨论】:

标签: php laravel


【解决方案1】:

要修改现有表,请创建新迁移。

php artisan make:migration alter_company_table

class AlterCompanyTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::table('company', function (Blueprint $table) {
            $table->integer('company_id')->nullable()->change();
            $table->integer('people_id')->nullable();
            $table->string('url')->nullable()->change();
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::table('company', function (Blueprint $table) {
            $table->integer('company_id')->nullable(false)->change();
            $table->dropColumn('people_id');
            $table->string('url')->nullable(false)->change();
        });
    }
}

【讨论】:

    【解决方案2】:

    如果您不介意丢失数据并且处于开发模式,您可以执行php artisan migrate:rollback 并在执行后:php artisan migrate。所以你的迁移工作正常。您可以阅读更多here

    现在,如果您只想添加新表并修改数据库中的其他表,则应该进行另一次迁移。但在此之前,您应该安装doctrine/dbal 才能正确修改您的表格,否则会给您带来很多错误。之后,将此行添加到您的 config/database.php

    use Illuminate\Database\DBAL\TimestampType;
    
    'dbal' => [
        'types' => [
            'timestamp' => TimestampType::class,
        ],
    ],
    

    现在您可以:php artisan make:migration add_new_tables_to_company 和您在database/migrations 中的文件。

    class AlterCompanyTable extends Migration
    {
        /**
         * Run the migrations.
         *
         * @return void
         */
        public function up()
        {
            Schema::table('company', function (Blueprint $table) {
                $table->integer('company_id')->nullable()->change();
                $table->integer('people_id')->nullable();
                $table->string('url')->nullable()->change();
            });
        }
    
        /**
         * Reverse the migrations.
         *
         * @return void
         */
        public function down()
        {
            Schema::table('company', function (Blueprint $table) {
                $table->integer('company_id')->nullable(false)->change();
                $table->dropColumn('people_id');
                $table->string('url')->nullable(false)->change();
            });
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2023-03-07
      • 2016-07-27
      • 1970-01-01
      • 1970-01-01
      • 2016-02-26
      • 2018-11-14
      • 2015-12-08
      • 2021-11-07
      • 2021-03-02
      相关资源
      最近更新 更多