繁体   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