Rails 3: Deleting a Table Using Migration

I have a table that I created using migration, now I want to get rid of this table. I am pretty sure that I can simply undo this migration, but I cannot find the syntax for this. I found this question when searching for Rails DB Migration - How to delete a table?

but he basically says that you can find what you need and provide a link. I read this link and I did not see anything that says how to do this. I saw him, but I don’t know how to collect them.

I see that during the migration process it has a self.down method, I just need to know what to call it.

+6
source share
5 answers

You can roll back the last migration using:

rake db:rollback

This will launch the self.down method, which should be drop_table :table_name

+8
source

Try creating an empty migration and use:

 drop_table :table_name 
+17
source
 rake db:rollback STEP=n 

where n is the number of steps you need to undo. If you leave STEP, it just rolls back.

To upgrade to a specific version, use:

 rake db:migrate:down VERSION=20080906120000 

If you want to quickly apply a table drop, you can create a new migration, start it, and then delete it along with the original migration that you no longer want. The syntax for deleting a table is:

 drop_table :table_name 
+3
source

Destroying a model is not the best way.

Instead, run this command in the rails: rake db:rollback console

(to access the rails console, enter rails c in the terminal as shown here )

0
source

You can delete the table with a rake to destroy the model:

 rails destroy model your_model 
-eleven
source

Source: https://habr.com/ru/post/895292/


All Articles