According to the rails documentation
http://guides.rubyonrails.org/migrations.html
2.3 Supported Type Modifiers says it should be possible to modify fields to allow or disallow NULL in the column, and that it's possible to do this on the terminal
This is what I want to appear in the migration file
class CreateTestModels < ActiveRecord::Migration
def change
create_table :test_models do |t|
t.string:non_nullable, :null => false
t.timestamps
end
end
end
On the terminal, I've tried
rails generate model TestModel non_nullable:string{null}
rails generate model TestModel 'non_nullable:string{null: false}'
I can't think of any other way to express it
Note: I already know you can go into the migration file and manually add it. That's not what I'm looking for.
You can open editor by utilising https://github.com/rails/rails/pull/38870 (available for Rails versions > 6.1.0)
To create migration with
null: false
from command line, first you need to enableEDITOR_FOR_GENERATOR
Than use
sed
to append to specific columns.For example that you want to create a model with
jti
andexp
columns with not null constrains and add index to them (index is supported on command line using:index
). We need to match linet.string :jti
and append to it so end result ist.string :jti, null: false
Here is command I use:
This works both for
rails g migration
andrails g model
.Resulting migration is