I'm an idiot...screwed up a migration in Rails:
thinking migrations would work like model generators (using references:modelname
) I did the following:
$ rails g migration add_event_to_photos references:event
which created the migration
class AddEventToPhotos < ActiveRecord::Migration
def change
add_column :photos, :references, :event
end
end
And now my development database (SQLite3) has a references
column of type event
in the photos
table.
And my schema.rb has a line in the middle saying:
# Could not dump table "photos" because of following StandardError
# Unknown type 'event' for column 'references'
rake db:rollback
is powerless against this:
$ rake db:rollback
== AddEventToPhotos: reverting ===============================================
-- remove_column("photos", :references)
rake aborted!
An error has occurred, this and all later migrations canceled:
undefine开发者_JAVA百科d method `to_sym' for nil:NilClass
So, how to roll back and maintain my development data in the database? I'd even be happy trashing the photos table if that's my only choice..but don't want to have to rebuild the whole thing. What to do?
btw- for anyone reading this about to make same stupid mistake...don't! Use the correct migration generator:
$ rails g migration add_event_to_photos event_id:integer
The easiest way I found to do this was to recreate the table in the schema.rb file in /db/
. Afterwards I ran a rake db:reset
(if it says you have pending migrations, just delete them and try again).
This took care of the problem.
Go into the database by ./script/rails dbconsole
. Then type these commands:
.output dump.sql
.dump
In the file dump.sql
you will have the SQL commands used to recreate and populate your database. Just edit it with your favourite editor (like vim
;-) removing or fixing the column type. You may also remove the invalid migration identifier from the schema_migrations
table. Drop your database (I suggest just rename the db/development.sqlite
file), create new database and read the dump file into it (using command .read dump.sql
).
Now you just need to fix and run your migrations.
add an empty down method and run rake db:rollback
edit ahh that's the new migration syntax, you can replace the body with simply:
def self.down; end
which is the old syntax, or perhaps delete the body altogether (haven't tried this) and then run rake db:rollback
Just an idea, I know it's not SQLite specific you can revert to an older version schema perhaps, load it up. And try again from there? You can revert (checkout) specific files in GIT. And then do def self.down; end
, as was suggested by another poster.
The problem arises because while SQLite will create the schema with whatever type you give it (in this case event
it can't dump that type back to ActiveRecord.
You need to edit the sqlite_master file and change create table string (sql
) to be the right thing.
You probably want to back up your table first since messing up that string will wreck your table if you do it wrong.
Here is a related rails issue
精彩评论