The Wild

Skip Rails Migration Files: Is It Possible?

· 4 min read · Mammals

In this article
  1. Why You Might Want to Skip Migrations
  2. How to Skip Migrations with Generators
  3. What About Skipping ActiveRecord Entirely?
  4. When Skipping Migrations Is a Bad Idea
  5. How to Remove a Migration After Generating It
  6. Alternatives to Migrations
  7. Frequently Asked Questions

Yes, you can generate models and other resources without creating migration files in Rails by using the --no-migration flag (or --skip-migration) with generators, or by passing --orm=false to skip ActiveRecord entirely. This is useful when you're working with an existing database, prototyping, or using a non-relational data store.

Why You Might Want to Skip Migrations

Migrations are Rails' built-in way to manage database schema changes over time. They're essential in most projects because they let teams version-control the database structure and apply changes consistently across environments. However, there are legitimate scenarios where generating a migration file is unnecessary or even problematic:

  • Existing database: You're connecting to a legacy or shared database that's already fully structured. You don't want Rails to create a new table or alter anything.
  • Schema-first workflow: Your team manages schema via SQL scripts or a separate tool like dbmate, Flyway, or Liquibase. Migrations would be redundant.
  • No database at all: You're using a non-relational store (e.g., Redis, MongoDB) or an external API as your data source. ActiveRecord isn't involved.
  • Prototyping: You just want a model class to test business logic without touching the database yet.

How to Skip Migrations with Generators

Rails generators accept a --no-migration flag that prevents the creation of a migration file. For example:

rails generate model Product name:string price:decimal --no-migration

This creates the Product model and its associated spec/test files, but no db/migrate/xxx_create_products.rb file. The same flag works for other generators that would normally produce migrations:

  • rails generate resource Product --no-migration – generates model, controller, routes, views, and no migration.
  • rails generate scaffold Product --no-migration – same as above but with full RESTful scaffold.

You can also use the long form --skip-migration, which is an alias. Both work in Rails 5 and later.

What About Skipping ActiveRecord Entirely?

If you don't need ActiveRecord at all, you can tell the generator to skip the ORM. This is common when you're using a different data layer, like MongoDB with Mongoid, or when you're building an API that talks to an external service.

rails generate model Product --orm=false

This produces a plain Ruby class in app/models/product.rb with no ActiveRecord inheritance, no migration, and no database-related methods. You can then include your own persistence logic or use a different library.

When Skipping Migrations Is a Bad Idea

In a typical Rails application that relies on ActiveRecord and a relational database, skipping migrations can cause problems:

  • Missing schema in new environments: If you don't create a migration, a new developer's db:schema.rb won't include the table. They'll get errors when running the app.
  • No rollback path: Migrations provide a way to revert schema changes. Without them, you have to manually write SQL to undo changes.
  • Schema drift: If you manually edit the database in one environment, others will quickly fall out of sync.
  • Testing issues: The test database is usually built from the schema, so missing migrations can break your test suite.

If you're working on a fresh Rails project with a normal database, it's almost always better to generate the migration and then edit it as needed. You can always delete the migration file later if you decide you don't need it.

How to Remove a Migration After Generating It

If you accidentally generated a migration and want to get rid of it, you can simply delete the file from db/migrate/. However, be careful:

  • If the migration has already been run (rails db:migrate), deleting the file without rolling back will leave your database out of sync with the schema.
  • To safely undo, run rails db:rollback first, then delete the file.
  • If you haven't run the migration, just delete the file and it's as if it never existed.

Alternatives to Migrations

If you're skipping migrations because you want a different schema management approach, here are common alternatives:

  • SQL files: Keep raw SQL schema files in a db/sql/ directory and apply them manually or with a tool.
  • Third-party migration tools: Tools like dbmate, Flyway, or Liquibase manage schema changes outside of Rails.
  • ActiveRecord schema dump: You can use rails db:schema:dump to generate schema.rb from an existing database, then load it into new environments with rails db:schema:load. This works without migrations.

Frequently Asked Questions

Does --no-migration work with all generators?

It works with any generator that would normally create a migration, such as model, resource, and scaffold. It has no effect on generators that don't create migrations, like controller or mailer.

Can I skip migrations for a specific model but keep them for others?

Yes, the flag is per command. You can run rails generate model User --no-migration and later run rails generate model Post without the flag to get a migration for Post.

What happens if I run rails db:migrate when there are no migration files?

Nothing happens. Rails will simply report that there are no migrations to run. Your schema.rb will reflect whatever is in the database, or be empty if the database is empty.

Is it possible to generate a migration later for a model that was created without one?

Yes, you can generate a migration manually with rails generate migration CreateProducts name:string price:decimal. Just make sure the migration matches the model attributes you defined earlier.

Does skipping migrations affect the test database?

Yes, because the test database is usually built from schema.rb, which is generated from migrations. If you skip migrations, your test database won't have the table unless you load it from elsewhere.