How To Modify / Update a Table?

Ok this is a general query. How do I update or modify a table and its accompanying files?

For example : I do a ruby script/generate scaffold TABLE_NAME FIELD_NAME1:TYPE FIELD_NAME2:TYPE

I then do a rake db:migrate and everything is generated. Cool. But now I realized I want to rename FIELD_NAME2 to FIELD_NAMEX ; what do I do?

Right now I go in and 1) delete every file generated by the previous generate and rake 2) then do the generate with renamed field 3) then rake all over again.

Now this almost always creates an error and it starts complaining that rake aborted table exists or it doesnt and in this case it is giving this error :

C:\ruby\letter4sure>rake db:migrate version=29 --trace (in C:/ruby/letter4sure) ** Invoke db:migrate (first_time) ** Invoke environment (first_time) ** Execute environment ** Execute db:migrate rake aborted! uninitialized constant CreatePsoAccounts c:/ruby/lib/ruby/gems/1.8/gems/activesupport-2.0.2/lib/active_support/dependencies.rb:266:in `load_missing_constant' c:/ruby/lib/ruby/gems/1.8/gems/activesupport-2.0.2/lib/active_support/dependencies.rb:453:in `const_missing' c:/ruby/lib/ruby/gems/1.8/gems/activesupport-2.0.2/lib/active_support/dependencies.rb:465:in `const_missing' c:/ruby/lib/ruby/gems/1.8/gems/activesupport-2.0.2/lib/active_support/inflector.rb:257:in `constantize' c:/ruby/lib/ruby/gems/1.8/gems/activesupport-2.0.2/lib/active_support/core_ext/string/inflections.rb:148:in `constantize' c:/ruby/lib/ruby/gems/1.8/gems/activerecord-2.0.2/lib/active_record/migration.rb:386:in `migration_class' c:/ruby/lib/ruby/gems/1.8/gems/activerecord-2.0.2/lib/active_record/migration.rb:363:in `migration_classes' c:/ruby/lib/ruby/gems/1.8/gems/activerecord-2.0.2/lib/active_record/connection_adapters/mysql_adapter.rb:286:in `inject' c:/ruby/lib/ruby/gems/1.8/gems/activerecord-2.0.2/lib/active_record/migration.rb:359:in `each' c:/ruby/lib/ruby/gems/1.8/gems/activerecord-2.0.2/lib/active_record/migration.rb:359:in `inject' c:/ruby/lib/ruby/gems/1.8/gems/activerecord-2.0.2/lib/active_record/migration.rb:359:in `migration_classes' c:/ruby/lib/ruby/gems/1.8/gems/activerecord-2.0.2/lib/active_record/migration.rb:339:in `migrate' c:/ruby/lib/ruby/gems/1.8/gems/activerecord-2.0.2/lib/active_record/migration.rb:311:in `down' c:/ruby/lib/ruby/gems/1.8/gems/activerecord-2.0.2/lib/active_record/migration.rb:300:in `migrate' c:/ruby/lib/ruby/gems/1.8/gems/rails-2.0.2/lib/tasks/databases.rake:85 c:/ruby/lib/ruby/gems/1.8/gems/rake-0.7.3/lib/rake.rb:392:in `call' c:/ruby/lib/ruby/gems/1.8/gems/rake-0.7.3/lib/rake.rb:392:in `execute' c:/ruby/lib/ruby/gems/1.8/gems/rake-0.7.3/lib/rake.rb:392:in `each' c:/ruby/lib/ruby/gems/1.8/gems/rake-0.7.3/lib/rake.rb:392:in `execute' c:/ruby/lib/ruby/gems/1.8/gems/rake-0.7.3/lib/rake.rb:362:in `invoke' c:/ruby/lib/ruby/gems/1.8/gems/rake-0.7.3/lib/rake.rb:355:in `synchronize' c:/ruby/lib/ruby/gems/1.8/gems/rake-0.7.3/lib/rake.rb:355:in `invoke' c:/ruby/lib/ruby/gems/1.8/gems/rake-0.7.3/lib/rake.rb:1739:in `top_level' c:/ruby/lib/ruby/gems/1.8/gems/rake-0.7.3/lib/rake.rb:1739:in `each' c:/ruby/lib/ruby/gems/1.8/gems/rake-0.7.3/lib/rake.rb:1739:in `top_level' c:/ruby/lib/ruby/gems/1.8/gems/rake-0.7.3/lib/rake.rb:1761:in `standard_exception_handling' c:/ruby/lib/ruby/gems/1.8/gems/rake-0.7.3/lib/rake.rb:1733:in `top_level' c:/ruby/lib/ruby/gems/1.8/gems/rake-0.7.3/lib/rake.rb:1711:in `run' c:/ruby/lib/ruby/gems/1.8/gems/rake-0.7.3/lib/rake.rb:1761:in `standard_exception_handling' c:/ruby/lib/ruby/gems/1.8/gems/rake-0.7.3/lib/rake.rb:1708:in `run' c:/ruby/lib/ruby/gems/1.8/gems/rake-0.7.3/bin/rake:7 c:/ruby/bin/rake:16:in `load' c:/ruby/bin/rake:16

Now this is awful. What I am doing wrong here? What I usually do when rake starts acting up is start deleting table and going back down the migration version as you can see above; but this one is still giving me the same error. Why is that?

Hey,

I think an easier way would be to generate a new migration file with "ruby script/generate migration RenameOldFieldNameFromTableName". Inside the "self.up" part of the migration file, you need to use the "rename_column :TableName, :OldFieldName, :NewFieldName". The "self.down" part should probably do the opposite "rename_column :TableName, :NewFieldName, :OldFieldName".

Once you finished that migration file, you can execute the migration with "rake db:migrate".

The following link gives an overview of all possible migration commands:

Falk

The classname in db/migrate is actually CreatePSOACCOUNTs!

I changed it to CreatePsoAccounts.

Then I did a rake db:migrate again and had to drop a table and this time I get the following name creation problem. Although I typed PSOAccounts rails decided to name it psoaccoun_ts ! what is that?

This synch. is still too inefficient, mechanical , brittle and complicated. I would like to work with someone in upgrading the rails plugin to automatically synchronize the model view with the db schema and the reverse.

Is anyone working on this or would like my input or assistance. I come from a Java/J2ee background.

Shiraz

Thorsten Mueller wrote:

I dread moving the application to the deployment server. Capistrano is not setup because deployment is on a windows server 2003 and synching dbs etc. is a problem. Am in too much of a hasty blurr to remember why but always often have trouble migrating to the production server with rakes.

I am serious about developing a useful GUI for rake. If anyone is interested we could work on making it more resilient and not so prone to errors.

Murphyslaw wrote:

Great its out of synch. again this time it keeps attempting to drop a table that does not exist! ? what do I do now? I have gone back some versions... should I create this table and see if it drops it?

This is not "joyful" , "painless" programming!

Is it?

Ather Shiraz wrote: