簡體   English   中英

如何在Laravel 5中將列更改為“非null”?

[英]How can I alter a column to be “not null” in Laravel 5?

這是一些示例代碼:

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

/**
 * Reverse the migrations.
 *
 * @return void
 */
public function down()
{
    Schema::table('orders', function(Blueprint $table)
    {
        $table->integer('user_id')->unsigned()->change();
    });
}

第一部分效果很好。 基本上,我要使用一個現有的user_id列並將其更改為可空值。 完美運作。

但是,當我運行migration:rollback時,該列保持可空狀態,並且上/下遷移關系不完善。 解決此問題的最佳方案是什么?

我建議這樣做的唯一方法是運行DB::statement()方法。 類似於以下內容

public function down() {
    DB::statement('ALTER TABLE `orders` MODIFY `user_id` INTEGER UNSIGNED NOT NULL;');
}

暫無
暫無

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

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