【问题标题】:Database migration notnull()?数据库迁移 notnull()?
【发布时间】:2017-09-12 22:15:58
【问题描述】:

我使用迁移将 nullable() 添加到列。

class ChangeUgIdCanNull extends Migration
{
  public function up()
  {
    Schema::table('service_request_step', function (Blueprint $table) {
      $table->dropForeign(['ug_id']);
    });
    Schema::table('service_request_step', function (Blueprint $table) {
      $table->dropIndex(['ug_id']);
    });
    Schema::table('service_request_step', function (Blueprint $table) {
      $table->integer('ug_id')->unsigned()->index()->nullable()->change();
      $table->foreign('ug_id')->references('ug_id')
            ->on('user_group')->onDelete('cascade');
    });
  }

  public function down()
  {
    Schema::table('service_request_step', function (Blueprint $table) {
      $table->dropForeign(['ug_id']);
    });
    Schema::table('service_request_step', function (Blueprint $table) {
      $table->dropIndex(['ug_id']);
    });
    Schema::table('service_request_step', function (Blueprint $table) {
      $table->integer('ug_id')->unsigned()->index()->change();
      $table->foreign('ug_id')->references('ug_id')
            ->on('user_group')->onDelete('cascade');
    });
  }
}

当我使用php artisan migrate 时很好。 但是,当我想php artisan migrate:rollback. 在我的数据库中的“ug_id”列中仍然可以为空。 我有没有像$table->integer('ug_id')->unsigned()->index()->notnull()->change();这样的功能

Laravel Version: 5.4.19
PHP Version: 7.1.3
Database Driver & Version: 10.2.4-MariaDB

【问题讨论】:

    标签: database-migration laravel-5.4


    【解决方案1】:

    您可以使用nullable(false)。 你的代码应该是

    $table->integer('ug_id')->unsigned()->index()->nullable(false)->change();
    

    【讨论】:

    • 哇。那是。谢谢。
    猜你喜欢
    • 2011-01-28
    • 1970-01-01
    • 2018-03-04
    • 2014-08-24
    • 2016-05-10
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多