Newbie Rails developer, can't get my database back to how it was before


I accidentally created a model with “rails generate model Category” (also creating migrate file) where I didn’t specify any fields. Then I run rake db:migrate to create the new table. Upon realising what I’d done, I run “rails destroy model Category”. Then I run “rails generate model Category name:string user_id:integer” which created all the new files. Ofcourse now db:migrate wouldn’t work coz I forgot I’d created the category table prior. So, I thought I’d remove the old table and run rake db:reset. This appeared to create all the tales again, but it keeps creating the OLD category table - the one without any fields. I’ve tried dropping the database, destroying the model/ migrate files, rolling back the migration, db:shema:load/ db:create:all/ . But every time, even if there is no “create category table” migrate file - it still creates the category table with no fields.

Is this normal? I don’t know where it’s getting the schema from but it can’t be reading only from my db:migrate files. Does it get them from log files when recreating the db, or does it look directly at the db and rebuild. I want my database build purely from the files I’m looking at in my db/migrate folder. How do I do this?

db:reset recreates the database from schema.rb, which is created by reading the state of the database (usually after you run migrations). The simplest possible thing is probably to just delete the bad categories table and let your new migration recreate it.


Thanks! But I really though I’d done that, must still have had the bad migration in there messing things up. All fine now.

Try generating your model and then adding a ‘drop_table’ command to your migration.

drop_table :categories if self.table_exists?(“categories”)

create_table :categories do |t| …