ruby on rails add a column after a specific column name
I doubt it allowed you to actually rake db:migrate
this migration, so you shouldn't have to roll back. Just remove the bottom three add_column
s and replace the top one with
add_column :patient_allergies, :reaction_id, :integer, after: :patient_id
and it should be fine to migrate. For future reference, here's what that command you entered should look like:
rails generate migration add_reaction_id_to_patient_allergies reaction_id:integer
The space before integer
made the generator think it was a new column. Sadly you can't use Ruby syntax (a => b
) on the command line either.
Adding a column before another one in Rails
You're able to insert a column at the front of your table by using the :first
option:
add_column :table_name, :column_name, :column_type, first: true
You can still use :after
to handle all other positioning cases.
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).
PostgreSQL: add_column after option usage in Rails migration
Your last definition is correct. But the problem here isn't with Rails, but with PostgreSQL, which doesn't allow to add a column at specific position. Read more: How can I specify the position for a new column in PostgreSQL?
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).
In a Rails Migration (MySQL), can you specify what position a new column should be?
This is now possible in Rails 2.3.6+ by passing the :after parameter
https://rails.lighthouseapp.com/projects/8994/tickets/3286-patch-add-support-for-mysql-column-positioning-to-migrations
To everyone that doesn't see the advantage in having this feature: do you never look at your database outside of the ORM? If I'm viewing in any sort of UI, I like having things like foreign keys, status columns, flags, etc, all grouped together. This doesn't impact the application, but definitely speeds up my ability to review data.
How can I rename a database column in a Ruby on Rails migration?
rename_column :table, :old_column, :new_column
You'll probably want to create a separate migration to do this. (Rename FixColumnName
as you will.):
bin/rails generate migration FixColumnName
# creates db/migrate/xxxxxxxxxx_fix_column_name.rb
Then edit the migration to do your will:
# db/migrate/xxxxxxxxxx_fix_column_name.rb
class FixColumnName < ActiveRecord::Migration
def self.up
rename_column :table_name, :old_column, :new_column
end
def self.down
# rename back if you need or do something else or do nothing
end
end
For Rails 3.1 use:
While, the up
and down
methods still apply, Rails 3.1 receives a change
method that "knows how to migrate your database and reverse it when the migration is rolled back without the need to write a separate down method".
See "Active Record Migrations" for more information.
rails g migration FixColumnName
class FixColumnName < ActiveRecord::Migration
def change
rename_column :table_name, :old_column, :new_column
end
end
If you happen to have a whole bunch of columns to rename, or something that would have required repeating the table name over and over again:
rename_column :table_name, :old_column1, :new_column1
rename_column :table_name, :old_column2, :new_column2
...
You could use change_table
to keep things a little neater:
class FixColumnNames < ActiveRecord::Migration
def change
change_table :table_name do |t|
t.rename :old_column1, :new_column1
t.rename :old_column2, :new_column2
...
end
end
end
Then just db:migrate
as usual or however you go about your business.
For Rails 4:
While creating a Migration
for renaming a column, Rails 4 generates a change
method instead of up
and down
as mentioned in the above section. The generated change
method is:
$ > rails g migration ChangeColumnName
which will create a migration file similar to:
class ChangeColumnName < ActiveRecord::Migration
def change
rename_column :table_name, :old_column, :new_column
end
end
Specifying column name in a references migration
Do it manually:
add_column :post, :author_id, :integer
but now, when you create the belongs_to statement, you will have to modify it, so now you have to call
def post
belongs_to :user, :foreign_key => 'author_id'
end
Related Topics
How to Source Environment Variables for a Command Shell in a Ruby Script
In Rails, How to Access Response.Body in a Action Before It Returns
How to Understand Sender and Receiver in Ruby
Rails 3 Actionmail Openssl::Ssl::Sslerror
How to Override Gemfile for Local Development
What's a Rails Plugin, or Ruby Gem, to Automatically Fix English Grammar
Rails 4 Activerecord Typeerror Nil Is Not a Symbol
Rails Script Segmentation Fault with Rvm
Rails 4.0.3 Generating Incorrect Asset Paths with Asset_Sync
Rails/Activerecord: Save Changes to a Model's Associated Collections
Extending a Class Method in a Module
What Is the Purpose of Redo and Retry Statements in Ruby
How to Use Objects with Xsi:Types in Savon
Script to Run Against Stdin If No Arg; Otherwise Input File =Argv[0]