Ruby on Rails: Adding Columns to Existing Database

Ruby on Rails: adding columns to existing database

As Speransky suggested, you should never modify old migration files. Rather you should create a new migration that adds the desired column. For instance, in this case you would run the following command in your app to create the new migration:

rails generate migration AddListIdColumnToIdeas list_id:integer

And Rails would generate the migration file automatically and the only thing left to do is run rake db:migrate.

If you insist on modifying the old migration file, you can add the column as you did and run the following:

rake db:drop
rake db:create
rake db:migrate

Which will destroy your current database, create a new one and run all the migrations (which will include your new column).

Adding a column to an existing table in a Rails migration

If you have already run your original migration (before editing it), then you need to generate a new migration (rails generate migration add_email_to_users email:string will do the trick).
It will create a migration file containing line:
add_column :users, email, string
Then do a rake db:migrate and it'll run the new migration, creating the new column.

If you have not yet run the original migration you can just edit it, like you're trying to do. Your migration code is almost perfect: you just need to remove the add_column line completely (that code is trying to add a column to a table, before the table has been created, and your table creation code has already been updated to include a t.string :email anyway).

Adding a new column to model

You are doing it Wrong.

The syntax should be

rails g migration add_column_first_name_to_users first_name:string

rails g migration add_column_last_name_to_users last_name:string

or simply

rails g migration add_first_name_to_users first_name:string
rails g migration add_last_name_to_users last_name:string

or

The best way is to generate them in single Command(@RSB said).

Rails 4: Add Multiple Columns to Existing Table

No not necessary. You can do

Assuming TableName is user

rails g migration AddColumnsToUser col1:integer col2:integer .. etc.

Add a column to an existing database?

If you need to create the real field in your database you can use migrations.

But in this case you can use virtiual attributes:

Create two methods in your model:

def full_name
[first_name, last_name].joun(' ')
end

def full_name=(name)
split = name.split(' ', 2)
self.first_name = split.first
self.last_name = split.last
end

And add to your view:

<p> Full name </p>  
<%= f.text_field :full_name %>

How to add a new column in an existing table in Rails 5?

You forgot to add datatype, below is the updated migration.

class ChangeJobsTable < ActiveRecord::Migration[5.0]
def change
add_column :jobs, :skills2, :string
end
end

How to add a new table/column to an existing table using mysql for a rails application?

You should do this by executing migrations. 

But, if you want directly , then you can execute the alter table statement in the mysql.

For example:

ALTER TABLE users
ADD email varchar(30);

Add a column in a Rails Active Record migration with an initial value based on other columns

I ended up adding the column using add_column and then using direct SQL to update the value of the column. I used direct SQL and not the model per this answer, since then it doesn't depend on the current state of the model vs. the current state of the table based on migrations being run.

class AddFullName < ActiveRecord::Migration
def up
add_column :my_table, :full_name, :text
execute "update my_table set full_name = concat(first_name, ' ', last_name)"
end

def down
remove_column :my_table, :full_name
end
end

That said, if there is a better or more idiomatic approach to this, I'm all ears.



Related Topics



Leave a reply



Submit