Deleting table from schema - Rails
If you create an empty migration by running: rails g migration DropInstalls
or: rails generate migration DropInstalls
You can then add this into that empty migration:
class DropInstalls < ActiveRecord::Migration
def change
drop_table :installs
end
end
Then run rake db:migrate
in the command line which should remove the Installs table
Note: db/schema.rb
is an autogenerated file. To alter the db structure, you should use the migrations instead of attempting to edit the schema file.
Removing table from schema.rb
If you want to really really remove it you should prepare a migration to drop the table.
If you just remove the model ActiveRecord will still find the table in db (and that's probably why you're seeing it in schema.rb
- if I am right that you mean file and not db schema)
EDIT:
So I tried to reproduce this, and as a result I ended up with following order of commands.
- first drop the table (make a new migration with
drop_table :locations
and run it) - then run
rails destroy model location
(you will get an error if you destroy model first) - run
rails c
so Rails picks up the db change and updatesschema.rb
Rails DB Migration - How To Drop a Table?
You won't always be able to simply generate the migration to already have the code you want. You can create an empty migration and then populate it with the code you need.
You can find information about how to accomplish different tasks in a migration here:
http://api.rubyonrails.org/classes/ActiveRecord/Migration.html
More specifically, you can see how to drop a table using the following approach:
drop_table :table_name
Unable to delete tables in schema.rb from rails app
Firstly check if the table is present...in rails console...
##to view all tables
ActiveRecord::Base.connection.tables
i use rails console
to delete tables directly...
ActiveRecord::Base.connection.execute("drop table table_name")
###========OR===============
ActiveRecord::Migration.drop_table(:table_name)
###=========OR===============
ActiveRecord::Migration.drop_table("table_name")
###=========WITH EXAMPLE===============
ActiveRecord::Base.connection.drop_table :subscriptions
Then,delete the migration file or rename it again by changing timestamp and again run rake db:migrate
Hope it helps :)
How to Delete Table from Database permanently in ROR
If you haven't pushed your code then the solution given by @Cryptex Technologies works fine. But if you have(i.e if you are using version control) then i won't recommend that approach. In that case you should create a new migration something like this:
class RemoveTable < ActiveRecord::Migration[5.2]
def up
drop_table :table_name
end
def down
create_table :table_name do |t|
t.string :field_name_1
t.text :field_name_2
t.timestamps
end
add_index :table_name, :field_name_1, unique: true
end
end
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
...to 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
end
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
end
class Book < ApplicationRecord
end
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)
book.update!(author: author.name)
end
# 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
end
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
end
end
Related Topics
What Are the Ruby's Object#Taint and Object#Trust Methods
How to Reinstall a Gem Using Bundler
Is There a Good Admin Generator for Ruby on Rails
How to Access a Ruby Module Method
Ruby Class with Static Method Calling a Private Method
Rails 4.2 - Sidekiq Not Sending Emails in Development
Rails -- Create and Create! Methods, Ror 3 Tutorial
Bundle Command Not Found in Linux Debian
Asserting That a Particular Exception Is Thrown in Cucumber
Loop Through Activerecord::Associations::Collectionproxy with Each
Get Today's Date in Jekyll with Liquid Markup
What Alternatives to Irb Are There
Rails: How to Show User's "Last Seen At" Time
How to Dynamically Define a Class Method Which Will Refer to a Local Variable Outside
How to Use Active Record Without Rails
Ruby: How to Pass All Parameters and Blocks Received by One Method to Another
Gem::Loaderror: Specified 'Sqlite3' for Database Adapter, But the Gem Is Not Loaded