【问题标题】:How to change database column 'null' to 'nullable' using laravel migration in mysql?如何在mysql中使用laravel迁移将数据库列'null'更改为'nullable'?
【发布时间】:2020-02-20 11:06:09
【问题描述】:

这是我的车辆表。我想通过迁移来更改我的数据库结构


use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class CreateVehiclesTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('vehicles', function (Blueprint $table) {
            $table->increments('id');
            $table->string('image')->nullable();
            $table->string('reg_no')->unique();
            $table->string('fuel_type');
            $table->string('capacity');
            $table->double('rate');
            $table->boolean('req_carrier');
            $table->date('service_date');
            $table->integer('service_freq_km');
            $table->integer('service_freq_months');
            $table->date('insurance_date');
            $table->integer('insurance_freq_months');
            $table->date('eco_date');
            $table->integer('eco_freq_months');
            $table->date('licence_date');
            $table->integer('licence_freq_months');
            $table->integer('current_company');
            $table->string('status')->default("available");
            $table->timestampsTz();

        });
    }

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

我想为这些列赋予可为空的值。 1.service_date 2.service_freq_km 3.service_freq_months

如何在 mysql 中将这些列更改为可为空?

【问题讨论】:

  • 您已经在使用nullable() 作为图像列。将它用于您希望默认为 null 的那些列。
  • @zahidhasanemon yes 当我第一次创建这个迁移时,我添加了 nullable()。现在我也希望它添加到这些列中。

标签: mysql laravel migration alter-table


【解决方案1】:

为alter table创建新的迁移类并使用这个up函数

public function up()
{
    Schema::table('vehicles', function (Blueprint $table) {
        $table->date('service_date')->nullable();
        $table->integer('service_freq_km')->nullable();
        $table->integer('service_freq_months')->nullable();

   });
}

Schema::table 用于更改表,Schema::create 用于创建新表

【讨论】:

  • @Rasshid 我已经迁移了迁移。现在我想把它改成$table->date('service_date')->nullable(); 怎么修改表格?
  • 更新表格composer require doctrine/dbal这个包是必需的
【解决方案2】:

您需要创建名为“2019_10_24_00000_update_vehicle_tables”的新迁移文件

if(Schema::hasTable('vehicles')) {
    Schema::table('vehicles', function($table)
    {
        $table->date('service_date')->nullable();
        $table->integer('service_freq_km')->nullable();
        $table->integer('service_freq_months')->nullable();
    });
}

【讨论】:

  • @alprnkesskekoglu 我应该在“up”函数中写这个吗?
  • 我忘记了对不起...你需要在“up”函数里面写
【解决方案3】:

安装软件包以更新表composer require doctrine/dbal

既然您已经迁移了迁移文件,您现在需要使用 artisan 命令创建一个新的迁移文件:

php artisan make:migration change_nullable_field_columns_to_vehicles_tables --table=vehicles

在新建的迁移文件中,添加以下代码

public function up() {
        Schema::table('vehicles', function (Blueprint $table) {
            $table->date('service_date')->nullable()->change();
            $table->integer('service_freq_km')->nullable()->change();
            $table->integer('service_freq_months')->nullable()->change();
        });
    }

//供php artisan down

public function down(){
            Schema::table('vehicles', function (Blueprint $table) {
                $table->date('service_date')->nullable(false)->change();
                $table->integer('service_freq_km')->nullable(false)->change();
                $table->integer('service_freq_months')->nullable(false)->change();
            });
}

现在可以执行迁移命令了

php artisan migrate

【讨论】:

  • composer require doctrine/dbal需要安装这个包才能完成你的回答
【解决方案4】:

您可以阅读有关 Modifying Columns 的文档。

如果你想要这些功能,你需要先安装这个包:

composer require doctrine/dbal

然后,您需要创建另一个迁移,例如:

2019_10_24_xxxxxx_change_columns_to_nullable_in_vehicles.php

<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

class ChangeColumnsToNullableInVehicles extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::table('vehicles', function (Blueprint $table) {
            $table->date('service_date')->nullable()->change();
            $table->integer('service_freq_km')->nullable()->change();
            $table->integer('service_freq_months')->nullable()->change();
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::table('vehicles', function (Blueprint $table) {
            $table->date('service_date')->nullable(false)->change();
            $table->integer('service_freq_km')->nullable(false)->change();
            $table->integer('service_freq_months')->nullable(false)->change();
        });
    }
}

【讨论】:

    【解决方案5】:
    $table->string('first_name')->default('DEFAULT');
    

    如果默认值应该为空,则改为可空。

    $table->string('name')->nullable();
    
    $table->string('name')->nullable()->default('NULL');
    
    $table->string('name')->nullable()->default(NULL);
    
    $table->string('name')->nullable()->default();
    

    【讨论】:

      猜你喜欢
      • 2019-06-16
      • 2023-02-06
      • 2011-03-12
      • 2021-11-14
      • 2019-10-10
      • 1970-01-01
      • 2012-02-17
      • 2019-07-19
      • 2019-12-14
      相关资源
      最近更新 更多