Rails is introducing a handy new feature in its migration generator: a shortcut for creating not null columns. This enhancement, submitted by David Heinemeier Hansson (DHH) himself, simplifies the process of defining required fields in your database schema.

The New Syntax

Previously, to create a not null column, you’d have to explicitly specify null: false in your migration. Now, you can use the ! symbol as a shortcut.

bin/rails generate migration CreateUsers email_address:string!:uniq password_digest:string!

This generates the following migration:

class CreateUsers < ActiveRecord::Migration[8.0]
  def change
    create_table :users do |t|
      t.string :email_address, null: false
      t.string :password_digest, null: false

      t.timestamps
    end
    add_index :users, :email_address, unique: true
  end
end