doutu4335
2018-11-19 19:04
浏览 37
已采纳

如何通过迁移在laravel 5.7中创建非空列?

How can I create non-null columns in laravel 5.7 by migrating?

$table->string('title','50')->nullable();

does not work.
What can I do?

  • 写回答
  • 好问题 提建议
  • 追加酬金
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • doudian6229 2018-11-19 19:11
    已采纳

    When you migrate to add a column to a table that already has records, you will need to make it nullable, or at least give it a default. Otherwise what would the value be for all of your current records?

    Worst case scenario if you have no default value, make it nullable. Then go through and set the value for each one, then remove the nullable.

    Your second migration (to remove nullability) would be like this:

    $table->string('title','50')->nullable(false)->change();
    
    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题