What is the command for removing an existing column from a table using migration?
The column I want to remove is: country:string
From the table: sample_apps
To remove a column with migration:
rails g migration Remove..From.. col1:type col2:type col3:type
In your case:
rails g migration RemoveCountryFromSampleApps country:string
This will generate the following migration in Rails 5.0:
class RemoveCountryFromSampleApps < ActiveRecord::Migration[5.0]
def change
remove_column :sample_apps, :country, :string
end
end
Create migration file:
$ rails generate migration RemoveCountryFromSampleApps country:string
In generated migration file:
class RemoveCountryFromSampleApps < ActiveRecord::Migration
def change
remove_column :sample_apps, :country, :string
end
end
Then run:
rake db:migrate
To remove a column(country here) from table(sample_case)
rails generate migration RemoveCountryfromSampleCase country:string
Above command should generate a file YYYYMMDDHHMMSS_remove_countryfrom_sample_case.rb. under db/migrate folder
class RemoveCountryFromSampleCase < ActiveRecord::Migration[5.0]
def change
remove_column :sample_case, :country, :string
end
end
In my case (I was doing it for more than two columns) only this appears
class RemoveCountryFromSampleCase < ActiveRecord::Migration[5.0]
def change
end
end
remove_column line was not there so I added it manually and then fired the command
rails db:migrate
and it worked for me.
References https://stackoverflow.com/a/2963582
&
Ruby guide on Active Record migration
https://edgeguides.rubyonrails.org/active_record_migrations.html
If want to remove the index too, you do with migration too:
rails g migration remove_post_id_from_comments post_id:integer:index
migration file:
class RemovePostIdFromComments < ActiveRecord::Migration
def change
remove_index :comments, :post_id
remove_column :comments, :post_id, :integer
end
end
then run: rake db:migrate
Do you need to specify the type?
Why not just remove_column :sample_apps, :country or remove_column :comments, :post_id for the samples here? Seems to work and removes a chance for error (text or string).
Related
I want to add a new column in one of my table in Rails 5. I recently renamed a column by using the following way:
rails g migration ChangeJobsTable
then in 20160802104312_change_jobs_table.rb:
class ChangeJobsTable < ActiveRecord::Migration[5.0]
def change
rename_column :jobs, :skills, :skills1
end
end
then
rails db:migrate
It worked fine, but now if I want to also add a new column skills2, do I need to do it like this?
class ChangeJobsTable < ActiveRecord::Migration[5.0]
def change
add_column :jobs, :skills2
end
end
You indeed forgot the datatype. You can also do it via the console in the future:
rails g migration AddSkills2ToJobs skills2:string
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
This worked for me and you can verify after in the schema
rails g migration add_skills2_to_ChangeJobsTable skills2:string
rake db:migrate
I am using ruby 1.8.7 and rails 1.2.6. (Its old I know. But I have to use it.) I need to add column to users table. I cant use rails generate migration with rails 1.2.6. I need to add a versioned db migrate file. How can I do that?
I want to add product column to users table. I created a file in the db/migrate folder with following contents.
class AddProductToUser < ActiveRecord::Migration
def self.up
add_column :users, :product, :string
end
def self.down
remove_column :users, :product
end
end
I used script/generate migration AddProductToUser. It gives an error as
undefined method 'cache' for Gem:Module.
Any pointers on how to run migration in rails 1.2.6(<2.x) will also be useful.
Your migrate file looks (almost) fine, does the filename match the class name, and does it have a sequence number at the beginning that follows on from the previous migrate?
Slight change:
# db/migrate/123_add_product_to_user.rb
class AddProductToUser < ActiveRecord::Migration
def self.up
add_column :users, :product, :string
end
def self.down
remove_column :users, :product # note sure where radius came from?
end
end
Then should just be run with rake migrate
More info at apidock
I've read a few guides on adding an attribute to a Model in rails, but none seem to specify the Model you're affecting in the Migration.
I'd like to add an image_url property to our coffees model, but the migration examples I've seen don't specify a model. What would I need to do to get this working correctly?
Well the migration api is pretty clear:
add_column :table, :column_name, :column_type
Example:
add_column :coffees, :image_url, :string
you create a new migration file to add image_url in Coffee models retaled table.
if you write
rails g migration AddImageUrlToCoffees image_url:string
then a migration file will be generated like
class AddImageUrlToCoffees < ActiveRecord::Migration
def change
add_column :coffees, :image_url, :string
end
end
or
class AddImageUrlToCoffees < ActiveRecord::Migration
def up
add_column :coffees, :image_url, :string
end
def down
remove_column :coffees, :image_url, :string
end
end
when you run rake db:migrate then it will add one more column image_url in coffees table and it can be access from model Coffee.
Run this command in the console
rails generate migration AddImageUrlToCoffees image_url:string
it will generate for you with
class AddImageUrlToCoffees < ActiveRecord::Migration
def change
add_column :coffees, :image_url, :string
end
end
Suppose I created a table table in a Rails app. Some time later, I add a column running:
rails generate migration AddUser_idColumnToTable user_id:string.
Then I realize I need to add user_id as an index. I know about the add_index method, but where should this method be called? Am I supposed to run a migration (if yes, which one ?), then adding by hand this method?
You can run another migration, just for the index:
class AddIndexToTable < ActiveRecord::Migration
def change
add_index :table, :user_id
end
end
If you need to create a user_id then it would be a reasonable assumption that you are referencing a user table. In which case the migration shall be:
rails generate migration AddUserRefToProducts user:references
This command will generate the following migration:
class AddUserRefToProducts < ActiveRecord::Migration
def change
add_reference :user, :product, index: true
end
end
After running rake db:migrate both a user_id column and an index will be added to the products table.
In case you just need to add an index to an existing column, e.g. name of a user table, the following technique may be helpful:
rails generate migration AddIndexToUsers name:string:index will generate the following migration:
class AddIndexToUsers < ActiveRecord::Migration
def change
add_column :users, :name, :string
add_index :users, :name
end
end
Delete add_column line and run the migration.
In the case described you could have issued rails generate migration AddIndexIdToTable index_id:integer:index command and then delete add_column line from the generated migration. But I'd rather recommended to undo the initial migration and add reference instead:
rails generate migration RemoveUserIdFromProducts user_id:integer
rails generate migration AddUserRefToProducts user:references
Add in the generated migration after creating the column the following (example)
add_index :photographers, :email, :unique => true
For references you can call
rails generate migration AddUserIdColumnToTable user:references
If in the future you need to add a general index you can launch this
rails g migration AddOrdinationNumberToTable ordination_number:integer:index
Generated code:
class AddOrdinationNumberToTable < ActiveRecord::Migration
def change
add_column :tables, :ordination_number, :integer
add_index :tables, :ordination_number, unique: true
end
end
You can use this, just think Job is the name of the model to which you are adding index cader_id:
class AddCaderIdToJob < ActiveRecord::Migration[5.2]
def change
change_table :jobs do |t|
t.integer :cader_id
t.index :cader_id
end
end
end
For those who are using postgresql db and facing error
StandardError: An error has occurred, this and all later migrations canceled:
=== Dangerous operation detected #strong_migrations ===
Adding an index non-concurrently blocks writes
please refer this article
example:
class AddAncestryToWasteCodes < ActiveRecord::Migration[6.0]
disable_ddl_transaction!
def change
add_column :waste_codes, :ancestry, :string
add_index :waste_codes, :ancestry, algorithm: :concurrently
end
end
I need to remove a few columns from my rails model which i already created and have some row entries in that model.
How to do it? Any links which has details for modifying the schema in rails ?
I'm using rails version 3.
To remove a database column, you have to generate a migration:
script/rails g migration RemoveColumns
Then in the self.up class method, remove your columns:
def self.up
remove_column :table_name, :column_name
end
You may want to add them back in the self.down class method as well:
def self.down
add_column :table_name, :column_name, :type
end
The Rails Guide for this goes into much more detail.
If you know the columns you want to remove you can use the convention: Remove..From.. when naming your migrations. Additionally you can include the column names when running the migration command.
The form of the command:
rails g migration Remove..From.. col1:type col2:type col3:type
For example:
rails g migration RemoveProjectIDFromProjects project_id:string
generates the following migration file:
class RemoveProjectIdFromProjects < ActiveRecord::Migration
def self.up
remove_column :projects, :project_id
end
def self.down
add_column :projects, :project_id, :string
end
end
Via command alternative as Add, only change Add to Remove:
Single Column:
rails g migration RemoveColumnFromTable column:type
Multiple Columns:
rails g migration RemoveColumn1AndColumn2FromTable column1:type colummn2:type