Rakesh
Rakesh

Reputation: 443

Rails Migration : adding an extra column to the table for which records are already loaded

I am using rails 2.3.8. I had created a migration for a table called user which currently has plenty of records.

Now the requirement is to add a new column to it.

I had to create a new migration file since rollback would delete the other table data. But adding a new migration did not work.

def self.up
     add_column "users", "xyz", :string, :default => "0"
end

What might be the reason?

Thanks in advance.

Upvotes: 0

Views: 834

Answers (2)

Nimesh Nikum
Nimesh Nikum

Reputation: 1839

Ben pointed out correctly. That might be due to older migration version number. But can you tell which type of error are you facing? And is it during running migration or what?

Upvotes: 0

Ben Lee
Ben Lee

Reputation: 53319

Following up on @elmt's comment, you should create migrations using the command in your rails root directory:

script/generate migration <your_migration_name>

So in your case that would be something like this:

script/generate migration add_xyz_to_users

This will create a filename of the form db/migrate/20101122183814_add_xyz_to_users.rb. That's where you should add your migration. If the filename is not named correctly, the migration will not run.

Upvotes: 1

Related Questions