How to update DB:migration file after adding a column

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

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

On 28 Aug., 10:37, Charles R. [email protected]
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.

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…

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…

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.

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.

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs