簡體   English   中英

在Laravel 5遷移中使列不可為空

[英]Make column not nullable in a Laravel 5 migration

我發現這個問題非常類似於我的Make列在Laravel遷移中不可為空,雖然它差不多3歲,它肯定不屬於Laravel 5

我的問題是我有一個遷移,它在up函數中修改了一個列使其可以為空。 現在,在down函數中我想讓它再次無法為空。

/**
 * Run the migrations.
 *
 * @return void
 */
public function up()
{
    Schema::table('mytable', function(Blueprint $table) {
        $table->string('mycolumn')->nullable()->change();
    });
}

/**
 * Reverse the migrations.
 *
 * @return void
 */
public function down()
{
    Schema::table('mytable', function(Blueprint $table) {
    /* THIS IS WHAT I WOULD EXPECT TO DO
    THOUGH OF COURSE THE FUNCTION notNullable DOES NOT WORK */
        $table->string('mycolumn')->notNullable()->change(); 
    });
}

我可以使用原始SQL實現這一點,但我想使用Laravel方法,如果可能的話......但是我找不到它,可能它還沒有在版本5中實現它。

將其重置為不可為空。 你可以試試這個。

/**
 * Run the migrations.
 *
 * @return void
 */
public function up()
{
    Schema::table('mytable', function(Blueprint $table) {
        $table->string('mycolumn')->nullable()->change();
    });
}

/**
 * Reverse the migrations.
 *
 * @return void
 */
public function down()
{
    Schema::table('mytable', function(Blueprint $table) {
    /* By default it's NOT NULL */
        $table->string('mycolumn')->nullable(false)->change(); // <--- here
    });
}

默認情況下它是NOT NULL,所以你應該嘗試這個。

/**
 * Run the migrations.
 *
 * @return void
 */
public function up()
{
    Schema::table('mytable', function(Blueprint $table) {
        $table->string('mycolumn')->nullable()->change();
    });
}

/**
 * Reverse the migrations.
 *
 * @return void
 */
public function down()
{
    Schema::table('mytable', function(Blueprint $table) {
    /* By default it's NOT NULL */
        $table->string('mycolumn')->change(); 
    });
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM