Ruby on rails How to remove added columns and insert new columns through a migration file -
hi created ruby on rails migration file follows , in first stage created tables
then want add columns , remove columns , modified follows
class createmt940batches < activerecord::migration def change create_table :mt940_batches |t| t.string :account_number t.string :transaction_reference_number t.string :information_to_account_owner t.string :file_name t.binary :raw_data_transaction t.string :sha1_checksum t.timestamps end def self.down remove_column :account_number, :transaction_reference_number, :information_to_account_owner end def self.up add_column :mt940_batches, :created_by, :updated_by, :integer end end end
but when ran rake db:migrate nothing has happens. how accomplish task . want change model created migration file. um looking way this. thank in advance
you should add remove / add column in separate migration file.
class foomigration < activerecord::migration def down remove_column :account_number, :transaction_reference_number, :information_to_account_owner end def add_column :mt940_batches, :created_by, :updated_by, :integer end end
please note up
, down
method should idem potent. should able go 1 other when calling rake db:migrate:down
, rake db:migrate:up
. not case here.
however here, seems want achieve 2 different things in single migration. if want add , remove columns, consider moving each 1 in different migration file:
please read here more details
you end 2 migrations file this:
class removefieldsfrommt940batches < activerecord::migration def change remove_column :mt940_batches, :account_number, :transaction_reference_number, :information_to_account_owner end end class addfieldstomt940batches < activerecord::migration def change add_column :mt940_batches, :created_by, :updated_by, :integer end end
Comments
Post a Comment