我可以将默认值传递给 rails 生成迁移吗? [英] Can I pass default value to rails generate migration?

查看:39
本文介绍了我可以将默认值传递给 rails 生成迁移吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道是否可以将默认值传递给 rails g migration 命令.类似的东西:

I want to know if I can pass a default value to the rails g migration command. Something like:

 $ rails generate migration add_disabled_to_users disabled:boolean:false #where false is default value for disabled attribute

为了生成:

class AddDisabledToUsers < ActiveRecord::Migration
  def change
    add_column :users, :disabled, :boolean, default: false
  end
end

推荐答案

您不能:https://guides.rubyonrails.org/active_record_migrations.html#column-modifiers

不能通过命令行指定空值和默认值.

null and default cannot be specified via command line.

唯一的解决方案是在迁移生成后对其进行修改.在 Rails 3 中是如此,在 Rails 6 中仍然如此

The only solution is to modify the migration after it's generated. It was the case in Rails 3, still the case in Rails 6

这篇关于我可以将默认值传递给 rails 生成迁移吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆