# How to update DB:migration file after adding a column

**URL:** <https://www.ruby-forum.com/t/how-to-update-db-migration-file-after-adding-a-column/113166>\
**Category:** Rails\
**Created:** [August 28, 2007, 10:37am UTC](https://www.ruby-forum.com/t/how-to-update-db-migration-file-after-adding-a-column/113166 "2007-08-28T10:37:50Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![scrajesh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/46a35a/32.png) [@scrajesh](https://www.ruby-forum.com/u/scrajesh)\
**Post date:** [August 28, 2007, 10:37am UTC](https://www.ruby-forum.com/t/how-to-update-db-migration-file-after-adding-a-column/113166/1 "2007-08-28T10:37:50Z")

</div>

Actually the migration files are created when a model for a table is  
generated. If suppose i need to add a new column to the existing table,  
how should i implement it??  
What i’ve done is, simply added a column directly to the table by  
logging into the MySQL. Then simply added the necessary column, but now  
i am stuck with sharing this table updation with other developers. How  
can i update the migration file now?? else kindly tell me the correct  
procedure…

thanx n regards

- Charles

---

<div class="post-metadata">

**Author:** ![scrajesh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/46a35a/32.png) [@scrajesh](https://www.ruby-forum.com/u/scrajesh)\
**Post date:** [August 28, 2007, 10:45am UTC](https://www.ruby-forum.com/t/how-to-update-db-migration-file-after-adding-a-column/113166/2 "2007-08-28T10:45:38Z")

</div>

Also is there any way to create a migration file after modifying an  
existing table?

---

<div class="post-metadata">

**Author:** ![scrajesh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/46a35a/32.png) [@scrajesh](https://www.ruby-forum.com/u/scrajesh)\
**Post date:** [August 28, 2007, 1:09pm UTC](https://www.ruby-forum.com/t/how-to-update-db-migration-file-after-adding-a-column/113166/3 "2007-08-28T13:09:59Z")

</div>

On 28 Aug., 10:37, Charles R. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> Actually the migration files are created when a model for a table is  
> generated. If suppose i need to add a new column to the existing table,  
> how should i implement it??

If you need to add a column to an existing table just create another  
migration that adds just this one column. This is what migrations were  
created for in the first place.

---

<div class="post-metadata">

**Author:** ![scrajesh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/46a35a/32.png) [@scrajesh](https://www.ruby-forum.com/u/scrajesh)\
**Post date:** [August 29, 2007, 3:10am UTC](https://www.ruby-forum.com/t/how-to-update-db-migration-file-after-adding-a-column/113166/4 "2007-08-29T03:10:13Z")

</div>

Martin M. wrote:

> If you need to add a column to an existing table just create another  
> migration that adds just this one column. This is what migrations were  
> created for in the first place.

Yes, we’ve to do so. But i don’t know how to implement it…!!  
I don’t know about this migration stuff, usually for any DB related  
modifications, i directly work in the MySQL server and send the query  
that helped to modify to the team-mates.  
Kindly tell me how to create a migration for this particular change on  
the particular table?? Please help…

---

<div class="post-metadata">

**Author:** ![scrajesh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/46a35a/32.png) [@scrajesh](https://www.ruby-forum.com/u/scrajesh)\
**Post date:** [August 29, 2007, 5:30am UTC](https://www.ruby-forum.com/t/how-to-update-db-migration-file-after-adding-a-column/113166/5 "2007-08-29T05:30:37Z")

</div>

Charles R. wrote:

> Martin M. wrote:
> 
> > If you need to add a column to an existing table just create another  
> > migration that adds just this one column. This is what migrations were  
> > created for in the first place.
> 
> Yes, we’ve to do so. But i don’t know how to implement it…!!  
> I don’t know about this migration stuff, usually for any DB related  
> modifications, i directly work in the MySQL server and send the query  
> that helped to modify to the team-mates.  
> Kindly tell me how to create a migration for this particular change on  
> the particular table?? Please help…

ruby script/generate migration add\_proj\_to\_qatest

will create a new migration (in my case it was the 26th)  
026\_add\_proj\_to\_qatest.rb

inside that, in the self.up, add your columns  
add\_column :tablename, :fieldname, :fieldtype  
like:

class AddProjToQatest \< ActiveRecord::Migration  
def self.up  
add\_column :qatests, :project\_id, :integer  
add\_column :qatests, :projectname, :string  
end

def self.down  
remove\_column :qatests, :project\_id  
remove\_column :qatests, :projectname  
end  
end

then just rake the migration…

---

<div class="post-metadata">

**Author:** ![scrajesh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/46a35a/32.png) [@scrajesh](https://www.ruby-forum.com/u/scrajesh)\
**Post date:** [August 29, 2007, 5:33am UTC](https://www.ruby-forum.com/t/how-to-update-db-migration-file-after-adding-a-column/113166/6 "2007-08-29T05:33:56Z")

</div>

Lets say you already have an orders table in your db.

## Hereâ€™s a migration that adds an e\_mail column to the orders table.

class AddEmailColumnToOrders \< ActiveRecord::Migration

def self.up  
add\_column :orders, :e\_mail, :string  
end

def self.down  
remove\_column :orders, :e\_mail  
end

end

* * *

I would use the migration instead of manipulating the mysql using either  
a front-end or a command line. With migration you have a way to  
roll-back and also can view all the changes that have been done on the  
db side.

All the best.

do some googling to see a few tutorials.

The example above is from the agile web development book.

---

<div class="post-metadata">

**Author:** ![scrajesh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/46a35a/32.png) [@scrajesh](https://www.ruby-forum.com/u/scrajesh)\
**Post date:** [August 29, 2007, 9:48am UTC](https://www.ruby-forum.com/t/how-to-update-db-migration-file-after-adding-a-column/113166/7 "2007-08-29T09:48:44Z")

</div>

Darius Azimi wrote:

> Lets say you already have an orders table in your db.
> 
> ## Hereâ€™s a migration that adds an e\_mail column to the orders table.
> 
> class AddEmailColumnToOrders \< ActiveRecord::Migration
> 
> def self.up  
> add\_column :orders, :e\_mail, :string  
> end
> 
> def self.down  
> remove\_column :orders, :e\_mail  
> end
> 
> end
> 
> * * *
> 
> I would use the migration instead of manipulating the mysql using either  
> a front-end or a command line. With migration you have a way to  
> roll-back and also can view all the changes that have been done on the  
> db side.
> 
> All the best.
> 
> do some googling to see a few tutorials.
> 
> The example above is from the agile web development book.

Thanx a lot guys!  
it worked great… will change my mode of approach in working with DB  
hereafter…  
really this migration feature eases the work.

again, thanx a lot!!

regards,

- Charles.
