Remove Rails Model After Migration

Rails remove old models with migrations

What about doing ruby script/destroy model? That should take care of the model and the migration.

Remove rails model after migration

rails d model name 

This just deletes the model and not the migration you have run (which created the table in the database).

If you want to delete both the model and the tables, you will have to do the following

rake db:rollback 
rails d model name

Remove Model and Table so can start again in Rails

In order to completely remove all columns &
tables that migration has created you need to run:

rails db:migrate:down VERSION=012345678 (where 012345678 should be the version number of your migration)


rails destroy model Comments

will delete your Model, pending migration, tests and fixtures

So destroy it's the opposite of generate:

$ bin/rails destroy model Oops
invoke active_record
remove db/migrate/20120528062523_create_oops.rb
remove app/models/oops.rb
invoke test_unit
remove test/models/oops_test.rb
remove test/fixtures/oops.yml

And, you can now create a new Model with the same name, as there's no trace of your previous one :)

How to completely remove a model and its associations in Rails?

About the unusual `schema.rb` behavior

I tried checking out an older commit, but the schema file stubbornly refuses to change.

This is quite unusual. Do verify that you're tracking the db/schema.rb file using git. If it is tracked, there's no reason why checking out an older commit shouldn't return it to the older state. At that point, you should be able to:

$ rails db:drop
$ rails db:create
$ rails db:schema:load load the old schema into the database. Then, you should be able to return to the latest code with git, and run pending migrations after the date at which the older schema was created.

About a cleaner way to implement this

Before writing the below migration, the first step would be to remove any existing relationship written in the Book class. For example:

# app/models/book.rb
class Book < ApplicationRecord
# The line below should be deleted! Otherwise, it will probably interfere
# with the `book.update!(author: ...)` line in the migration.
belongs_to :author

I've taken to writing related migrations in a single file, since they're all related. To me, this looks like:

class MoveAuthorToBooks < ActiveRecord::Migration[6.0]
class Author < ApplicationRecord

class Book < ApplicationRecord

def up
# Start by adding a string column.
add_column :books, :author, :string

# Let's preserve existing author names.
Book.all.each do |book|
author = Author.find(book.author_id)

# Now that the names have been moved to the books table, we don't
# need the relationship to `authors` table anymore. This should
# also delete any related foreign keys - manual foreign key deletion
# should not be required.
remove_column :books, :author_id

# Alternative: If you'd created the `authors_id` column using the
# `add_reference` command, then it's probably best to use the opposite
# `remove_reference` command.
#remove_reference :books, :author, index: true, foreign_key: true

# Finally, remove the `authors` table.
drop_table :authors

def down
# This can be technically be reversed, but that'll need some more code that
# reverses the action of the `up` function, and it may not be needed.

raise ActiveRecord::IrreversibleMigration

How to delete migration files in Rails 3

I usually:

  1. Perform a rake db:migrate VERSION=XXX on all environments, to the version before the one I want to delete.
  2. Delete the migration file manually.
  3. If there are pending migrations (i.e., the migration I removed was not the last one), I just perform a new rake db:migrate again.

If your application is already on production or staging, it's safer to just write another migration that destroys your table or columns.

Another great reference for migrations is:

Add migration and then delete migration file

As long as you keep the schema.rb file, you are free to delete the old migration files. (But first make sure you have run the migrations before deleting them!)

You can use rake db:schema:dump to create an up to date schema file.

Related Topics

Leave a reply