# How To Modify / Update a Table?

**URL:** <https://www.ruby-forum.com/t/how-to-modify-update-a-table/137066>\
**Category:** Rails\
**Created:** [May 8, 2008, 5:34pm UTC](https://www.ruby-forum.com/t/how-to-modify-update-a-table/137066 "2008-05-08T17:34:41Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![ror\_arch](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/e19adc/32.png) [@ror\_arch](https://www.ruby-forum.com/u/ror_arch)\
**Post date:** [May 8, 2008, 5:34pm UTC](https://www.ruby-forum.com/t/how-to-modify-update-a-table/137066/1 "2008-05-08T17:34:41Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![ror\_arch](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/e19adc/32.png) [@ror\_arch](https://www.ruby-forum.com/u/ror_arch)\
**Post date:** [May 8, 2008, 5:52pm UTC](https://www.ruby-forum.com/t/how-to-modify-update-a-table/137066/2 "2008-05-08T17:52:44Z")

</div>

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:

> **[ActiveRecord::Migration](https://api.rubyonrails.org/classes/ActiveRecord/Migration.html)**
>
> Active Record Migrations Migrations can manage the evolution of a schema used by several physical databases.

Falk

On 8 Mai, 17:34, Ather S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![ror\_arch](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/e19adc/32.png) [@ror\_arch](https://www.ruby-forum.com/u/ror_arch)\
**Post date:** [May 8, 2008, 5:56pm UTC](https://www.ruby-forum.com/t/how-to-modify-update-a-table/137066/3 "2008-05-08T17:56:47Z")

</div>

you get this error often if your classname doesn’t match the filename  
(eg often happens if you copy from one file into another)

if your classname is:  
CreatePsoAccounts

about this error complains, then your file MUST be named like

00x\_create\_pso\_accounts.rb

(the number is variable of course, the rest not)

how to handle such changes depends on your project stage  
if it’s in the beginning and no live system with real data exists, you  
can work as you do, just correct the old migrations.

but if there is a db with real data, then you must use

change\_column  
rename\_column  
and so on.  
rails has a full set of commands to change your db structure

(another reason not to change old migrations is if you work in a team  
and other people have to use your code. then they often forget to  
rebuild the db completely after updating their code.)

---

<div class="post-metadata">

**Author:** ![ror\_arch](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/e19adc/32.png) [@ror\_arch](https://www.ruby-forum.com/u/ror_arch)\
**Post date:** [May 8, 2008, 6:57pm UTC](https://www.ruby-forum.com/t/how-to-modify-update-a-table/137066/4 "2008-05-08T18:57:29Z")

</div>

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 M. wrote:

> you get this error often if your classname doesn’t match the filename  
> (eg often happens if you copy from one file into another)
> 
> if your classname is:  
> CreatePsoAccounts
> 
> about this error complains, then your file MUST be named like
> 
> 00x\_create\_pso\_accounts.rb
> 
> (the number is variable of course, the rest not)
> 
> how to handle such changes depends on your project stage  
> if it’s in the beginning and no live system with real data exists, you  
> can work as you do, just correct the old migrations.
> 
> but if there is a db with real data, then you must use
> 
> change\_column  
> rename\_column  
> and so on.  
> rails has a full set of commands to change your db structure
> 
> (another reason not to change old migrations is if you work in a team  
> and other people have to use your code. then they often forget to  
> rebuild the db completely after updating their code.)

---

<div class="post-metadata">

**Author:** ![ror\_arch](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/e19adc/32.png) [@ror\_arch](https://www.ruby-forum.com/u/ror_arch)\
**Post date:** [May 8, 2008, 8:40pm UTC](https://www.ruby-forum.com/t/how-to-modify-update-a-table/137066/5 "2008-05-08T20:40:55Z")

</div>

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 S. 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:
> 
> > 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:  
> > [ActiveRecord::Migration](http://api.rubyonrails.org/classes/ActiveRecord/Migration.html)
> > 
> > Falk

---

<div class="post-metadata">

**Author:** ![ror\_arch](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/e19adc/32.png) [@ror\_arch](https://www.ruby-forum.com/u/ror_arch)\
**Post date:** [May 8, 2008, 7:29pm UTC](https://www.ruby-forum.com/t/how-to-modify-update-a-table/137066/6 "2008-05-08T19:29:44Z")

</div>

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:

> 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:  
> [ActiveRecord::Migration](http://api.rubyonrails.org/classes/ActiveRecord/Migration.html)
> 
> Falk
