← Home

Rails Generate migration

Reference & Command Builder

Published: 2023-05-11
Author: Harrison Broadbent
Type: Reference & Command Builder

Rails Generate Migration

Rails Generate Migration (rails g migration) is the generator for an ActiveRecord migration. Migrations define mutations to your database schema, including adding and removing database tables, columns and indexes.

The Rails migration generator will do it's best to infer the contents of your migration from the migration name you pass, and the fields.

For example, if your run the following generator —

rails g migration AddNameToUser name:string:index

Rails will automatically generate the following migration —

class AddNameToUser < ActiveRecord::Migration[7.0]
  def change
    add_column :users, :name, :string
    add_index :users, :name

There's too much information on Rails Migrations to cover it all here. If you'd like more information though, I've written a Rails Generate Migration Reference Guide that you might find handy.

For your reference, ActiveRecord migrations (and hence all model-based generators) support the following data types —

:primary_key  # A unique key that identifies a model record.
:string       # Short text such as titles.
:text         # Long text data such as documents of information.
:integer      # For storing whole numbers.
:float        # For storing floating-point numbers.
:decimal      # For storing decimal numbers.
:datetime     # Stores both date and time.
:timestamp    # Stores both date and time.
:time         # Stores only the time.
:date         # Stores only the date.
:binary       # For storing binary data blobs like images.
:boolean      # True or false values.

Command Builder for rails g migration

rails g migration 

Migration Name

Migration Field(s)

Generator Options

Runtime Options

# Command Options
❯ rails g migration

  rails generate migration NAME [field[:type][:index] field[:type][:index]] [options]

      [--skip-namespace], [--no-skip-namespace]              # Skip namespace (affects only isolated engines)
      [--skip-collision-check], [--no-skip-collision-check]  # Skip collision check
  -o, --orm=NAME                                             # ORM to be invoked
                                                             # Default: active_record

ActiveRecord options:
        [--timestamps], [--no-timestamps]      # Indicates when to generate timestamps
                                               # Default: true
        [--primary-key-type=PRIMARY_KEY_TYPE]  # The type for primary key
  --db, [--database=DATABASE]                  # The database for your migration.
                                               # (By default, the current environment's primary database is used.)

Runtime options:
  -f, [--force]                    # Overwrite files that already exist
  -p, [--pretend], [--no-pretend]  # Run but do not make any changes
  -q, [--quiet], [--no-quiet]      # Suppress status output
  -s, [--skip], [--no-skip]        # Skip files that already exist

# Command Description
    Generates a new database migration. Pass the migration name, either
    CamelCased or under_scored, and an optional list of attribute pairs as arguments.

    A migration class is generated in db/migrate prefixed by a timestamp of the current date and time.

    You can name your migration in either of these formats to generate add/remove
    column lines from supplied attributes: AddColumnsToTable or RemoveColumnsFromTable

    `bin/rails generate migration AddSslFlag`

    If the current date is May 14, 2008 and the current time 09:09:12, this creates the AddSslFlag migration

    `bin/rails generate migration AddTitleBodyToPost title:string body:text published:boolean`

    This will create the AddTitleBodyToPost in db/migrate/20080514090912_add_title_body_to_post.rb,
    with this in the Change migration:

      add_column :posts, :title, :string
      add_column :posts, :body, :text
      add_column :posts, :published, :boolean

Migration names containing JoinTable will generate join tables for use with
has_and_belongs_to_many associations.

    `bin/rails g migration CreateMediaJoinTable artists musics:uniq`

    will create the migration

    create_join_table :artists, :musics do |t|
      # t.index [:artist_id, :music_id]
      t.index [:music_id, :artist_id], unique: true