Questions tagged [rails-migrations]

Rails migrations are used to track and apply database alterations in a reversible manner.

Migrations are a way to alter your database in a reversible manner using (usually) Ruby classes and objects rather than raw SQL. Rails also keeps track of which migrations have run so a simple

$ rake db:migrate

is all that is needed to bring your database up to date even if different people have made different changes. The migrations also maintain your db/schema.rb file.

Links:

1033 questions
67
votes
1 answer

How can i remove a column from table using rails console

It is easily possible to remove a column using rails migration. class SomeClass < ActiveRecord::Migration def self.up remove_column :table_name, :column_name end end I want to know if there is any way to remove a column from table using…
66
votes
4 answers

Rails Migration with adding and removing reference

After creating a migration file with rails generate migration AddClientToUser I can edit my migration file like so: class AddClientToUser < ActiveRecord::Migration def self.up change_table :users do |t| t.references :client end …
Matt Connolly
  • 9,757
  • 2
  • 65
  • 61
65
votes
6 answers

What's the correct syntax for remove_index in a Rails 3.1.0 migration?

I'm in the process of adding Devise to an existing Rails app, with a Users table already defined. The devise generator pushed out the following migration: class AddDeviseToUsers < ActiveRecord::Migration def self.up change_table(:users) do…
doublea
  • 2,546
  • 1
  • 23
  • 35
64
votes
3 answers

How do you make remove_column reversible?

I have a migration that removes a column: def change remove_column :foos, :bar, :boolean end When I try to rake db:rollback that migration, I get the following error: remove_column is only reversible if given a type. The ActiveRecord::Migration…
user1454117
61
votes
12 answers

What is the best way to resolve Rails orphaned migrations?

I have been switching between branches in a project and each of them have different migrations... This is the scenario: $ rake db:migrate:status Status Migration ID Migration Name -------------------------------------------------- ... …
Adrian
  • 9,102
  • 4
  • 40
  • 35
61
votes
4 answers

Rails naming convention for join table

This questions stems from: How to link form after creating rails join table I am creating the join table between my Product and Category Models. What should the join table be named? categories_products or category_products or something else?
60
votes
6 answers

Will removing a column with a Rails migration remove indexes associated with the column

In Rails 2, will removing a column with a Rails migration also change/remove indexes associated with the column? If not, and instead you have to also change/remove each index manually, shouldn't it instead be automated? Thanks (from a Rails newbie)
Dexygen
  • 12,287
  • 13
  • 80
  • 147
57
votes
2 answers

Gem::LoadError: can't activate pg (~> 0.18), already activated pg-1.0.0

I've been doing the Rails tutorial found here and have been successful up to the point of having to migrate the Comments migration using $ rails db:migrate. Prior to this point, I've been able to generate the Article model and migrate the Articles…
gangelo
  • 3,034
  • 4
  • 29
  • 43
55
votes
4 answers

When I run the rake:db migrate command I get an error "Uninitialized constant CreateArticles"

I created a model ruby script/generate model Article (simple enuff) Here is the migration file create_articles.rb: def self.up create_table :articles do |t| t.column :user_id, :integer t.column :title, :string t.column :synopsis,…
featureBlend
  • 667
  • 1
  • 8
  • 10
55
votes
2 answers

What is the best way to drop a table & remove a model in Rails 3?

I have a model & a table which I no longer need in my App, I could leave them there but I would like to remove them to keep things tidy. I'm trying to figure out the best way to remove them with out messing around with my migrations & db/schema.rb…
Holly
  • 7,462
  • 23
  • 86
  • 140
53
votes
6 answers

In a rails migration, how can you remove the limit of a field

Is the following correct? change_column :tablename, :fieldname, :limit => null
kidbrax
  • 2,364
  • 3
  • 30
  • 38
51
votes
2 answers

Removing default value from Rails Migration

I found several similar questions about editing a migration but couldn't figure this one out. I did a rails migration, then opened the migration file and added a default value option to the field. Then ran rake db:migrate. The default value…
Moosa
  • 3,126
  • 5
  • 25
  • 45
51
votes
4 answers

Rails 4: Remove not null constraint from table column with migration?

Given the following schema.rb: create_table "people", force: true do |t| t.string "name", null: false t.integer "age" t.integer "height" t.string "email" t.boolean "married", default: false t.text "bio" …
gabethegrape
  • 681
  • 1
  • 6
  • 11
50
votes
3 answers

How to create a migration to remove an index only if it exists, rather than throwing an exception if it doesn't?

Right now, the current migration might fail, if the books table doesn't have created_at or updated_at fields: class AddTimestampIndexes < ActiveRecord::Migration def up remove_index :books, :created_at remove_index :books, :updated_at …
TheFooProgrammer
  • 2,439
  • 5
  • 28
  • 43
48
votes
5 answers

How do I check the Database type in a Rails Migration?

I have the following migration and I want to be able to check if the current database related to the environment is a mysql database. If it's mysql then I want to execute the SQL that is specific to the database. How do I go about this? class…
Shaun
  • 4,057
  • 7
  • 38
  • 48
1
2
3
68 69