Remove Model and Table so can start again in Rails
Asked Answered
S

3

7

I created a model for comments at the start of a project, but have now come to the realisation I need to create some polymorphic relations so I can use comments with a number of other models as well. Considering the the code I already have etc, I'm thinking it might be easier for me to just start again from scratch so I can build all the views/controllers etc in the correct way for my new polymorphic world.

I see that I can run rails destroy model comments to achieve this but I have two questions on that:

  1. Will this delete the model, migrations AND the actual DB table?
  2. What are the implications when I want to create a new model with the exact same name?
Sowers answered 11/4, 2017 at 18:15 Comment(1)
You have the solution here to drop the database.Maurili
O
19

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 :)

Obliquely answered 11/4, 2017 at 19:12 Comment(1)
I'm not getting this behavior in Rails 6. the destroy model generator does not remove the create migration for the associated model. And it shouldn't! Rails isn't analyzing subsequent migrations to ensure that they don't rely on the destroyed model. The Rails way here is to destroy the model and if you want to get rid of the associated table, create a new db migration with the line drop_table :table_name. This preserves the integrity of your migration history, which is intended to be the source of truth for your schema. Always go forward with your migrations.Presber
S
5

If you have already migrated the database after creating the model:

First, rollback the changes to the database:

rake db:migrate:down VERSION=20100905201547

where version is the timestamp identifying the migration. For example, if your migration file is called 20170411182948_create_comments.rb then your version parameter should be 20170411182948

Then run

rails destroy model comments

The first command will delete the table from the actual database. The second command will delete the model and the migration file. Make sure you run them in that order as the first command is dependent on the migration file to perform the rollback (which is deleted during the second command).

If you have have not migrated the database:

The table would not have been added to your database. You can go ahead and delete your model and migration files manually or use the destroy command.

Supinate answered 11/4, 2017 at 18:42 Comment(0)
M
0

You might need to remove the table thoroughly.
Run this :

sqlite3 db/development.sqlite3 

Then :

sqlite> drop table table_name;
sqlite> .quit
Mathers answered 19/1, 2019 at 16:35 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.