简体   繁体   English

如何在Laravel 5中将列更改为“非null”?

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

Here's some sample code: 这是一些示例代码:

/**
 * 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();
    });
}

The first part works great. 第一部分效果很好。 Basically, I'm taking an existing user_id column and altering it so that it's nullable. 基本上,我要使用一个现有的user_id列并将其更改为可空值。 Works perfectly. 完美运作。

However, when I run migrate:rollback, the column stays nullable and I have an imperfect up/down migration relationship. 但是,当我运行migration:rollback时,该列保持可空状态,并且上/下迁移关系不完善。 What's the best practice solution for resolving this? 解决此问题的最佳方案是什么?

The only way I would suggest doing this is to run the DB::statement() method. 我建议这样做的唯一方法是运行DB::statement()方法。 Something like the following 类似于以下内容

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