Reputation: 5603
I had a rails Model List
.
I typed rails d model list
in my terminal, resulting in this:
invoke active_record
remove db/migrate/20140116161958_create__lists.rb
remove app/models/list.rb
invoke rspec
remove spec/models/list_spec.rb
Then I typed rails g model list name:string size:integer
which gave me this:
invoke active_record
create db/migrate/20140213155321_create_lists.rb
create app/models/list.rb
invoke rspec
create spec/models/list_spec.rb
Now, running rake db:migrate
gives me this:
== CreateLists: migrating ===============================================
-- create_table(:lists)
rake aborted!
An error has occurred, this and all later migrations canceled:
PG::DuplicateTable: ERROR: relation "lists" already exists
The issue is that my table was not deleted from my DB. I can't roll back the migration that created that table, because it was destroyed when i ran rails d model list
.
I could create a new migration and drop the table, but it would be placed after my migration created when I ran rails g model list...
, so I assume it would error too.
Is my only choice to delete the model again, create a migration to drop the table, then recreate the model?
Also, in the future, how should one go about deleting and recreating a model? Roll back the migration prior to rails d model
?
Upvotes: 0
Views: 426
Reputation: 3427
1>
before running rails d model list
run
$ rake db:migrate:down VERSION=20140116161958
will roll back the list file to remove table lists from database.
2>
but since u have already destroyed your model what u can do is delete table lists from rails database console. try this
$ rails dbconsole # from your app root path
and then type drop table lists;
3>
you can drop your table from rails console also
$rails console
Then just type:
ActiveRecord::Migration.drop_table(:lists)
4>
also u can create a migration file to drop your table :
$ rails generate migration DropListsTable
this will create an empty migration file now edit that file to look like:
class DropListsTable < ActiveRecord::Migration
def up
drop_table :lists
end
def down
raise ActiveRecord::IrreversibleMigration
end
end
then run $ rake db:migrate
Upvotes: 4