views:

2575

answers:

4

I ran "rake db:migrate" to re-sync schema.db with my database schema. But it failed, saying that one of my tables already exists. I think it was trying to re-create the table. If you just want to get schema.rb updated to reflected any changes you made in the database independently of Rails, what command should you use if not "rake db:migrate"? And what's the best source of documentation on this type of thing?

+1  A: 

Try rake db:schema:dump or rake db:migrate:redo.

pts
+3  A: 

"rake db:migrate" will try and run all the outstanding migrations for your project. If you just want to dump the schema do a "rake db:schema:dump".
But I think you have a problem where it says that the table already exists. One of your migrations is failing because the table it is trying to add already exists in your db. Did you create one by hand? Did you down a migration, but not have a down written for it? You will need to fix this before you can write future migrations. If it is just a mistake, and the table is there and correct and you want to ignore this. My recommendation is to hack around it by commenting out the create table in the failing migration. Then run "rake db:migrate". Then but the create table back. This will update your schema version.
Make sure you write proper downs on all migrations.

ScottD
Good explanation. Thanks.
drizzle
A: 

answering your last question regarding documentation:

Maximiliano Guzman
+1  A: 

I've found that occasionally, when things get a little weird, you'll find yourself in a situation where Rails will want to run a migration that it ought rightly to be considering already done (the table already exists, etc). You can mark a migration as done by finding its number (the number part at the beginning of the filename), going into mysql and issuing a query like so:

insert into schema_migrations values('20090521153438');

(or whatever the number of your migration is)

Or if it's a plugin migration being run using Desert's migrate_plugin:

insert into plugin_schema_migrations values('my_plugin', '005');
centipedefarmer
Thanks for the tip.
Espen