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
46
votes
7 answers

How do you skip failed migrations? (rake db:migrate)

I can't seem to find an option or anything that allows me to skip migrations. I know what you're thinking: "you should never have to do that..." I need to skip a migration that makes changes to specific user records that don't exist in my…
hmind
  • 840
  • 1
  • 6
  • 15
46
votes
7 answers

Rails: Is it bad to have an irreversible migration?

When is it acceptable to raise an ActiveRecord::IrreversibleMigration exception in the self.down method of a migration? When should you take the effort to actually implement the reverse of the migration?
readonly
  • 343,444
  • 107
  • 203
  • 205
45
votes
4 answers

Can I pass default value to rails generate migration?

I want to know if I can pass a default value to the rails g migration command. Something like: $ rails generate migration add_disabled_to_users disabled:boolean:false #where false is default value for disabled attribute in order to generate: class…
Leantraxxx
  • 4,506
  • 3
  • 38
  • 56
44
votes
2 answers

Best SQL indexes for join table

With performance improvements in mind, I was wondering if and which indexes are helpful on a join table (specifically used in a Rails 3 has_and_belongs_to_many context). Model and Table Setup My models are Foo and Bar and per rails convention, I…
Aaron
  • 13,349
  • 11
  • 66
  • 105
39
votes
4 answers

varchar Migration question for Ruby on Rails

I have created a new table including a column "note". The default is varchar(255) I believe but I wish to have this column be a text area vs. a field and to allow more data. I imagine that I would make this change in ActiveRecord::Migration file but…
bgadoci
  • 6,363
  • 17
  • 64
  • 91
38
votes
4 answers

How can I remove a unique constraint from a database column in Rails?

I created a table using the following migration: class CreateProfilePictures < ActiveRecord::Migration def change create_table :profile_pictures do |t| t.integer :user_id, null: false t.integer :picture_id, null: false …
Daniel
  • 1,284
  • 3
  • 12
  • 17
37
votes
5 answers

rails 3.2 migration cannot add index to create_table in change method

here is my migration in rails 3.2.2: class CreateStatistics < ActiveRecord::Migration def change create_table :statistics do |t| t.string :name t.integer :item_id t.integer :value t.text :desc t.timestamps …
linjunhalida
  • 4,538
  • 6
  • 44
  • 64
37
votes
4 answers

Rails : migration for creating a fixed-length char(12) column

What is the best way to define a fixed-length SQL column (CHAR(12) for instance) through a Rails migration ? Why this should not handled by the model is because of the performance of char() vs varchar(), and I'd like to avoid injecting raw SQL in…
manu_v
  • 1,248
  • 2
  • 12
  • 21
37
votes
2 answers

How do I add migration with multiple references to the same model in one table? Ruby/Rails

How do I create a migration with two fields that reference the same table? I have tables A, and image. A.image1_id will reference image, and A.image2_id will reference image also. There are only 2 images, not many. If I use class AddFields <…
Chloe
  • 25,162
  • 40
  • 190
  • 357
36
votes
1 answer

Add a reference column migration in Rails 5

A user has many uploads. I want to add a column to the uploads table that references the user. What should the migration look like? Related question for Rails 3: Rails 3 migrations: Adding reference column? Related question for Rails 4: Add a…
kkurian
  • 3,844
  • 3
  • 30
  • 49
34
votes
7 answers

How do I move a column (with contents) to another table in a Rails migration?

I need to move some columns from one existing table to another. How do I do it using a rails migration? class AddPropertyToUser < ActiveRecord::Migration def self.up add_column :users, :someprop, :string remove_column :profiles,…
Eero
  • 4,704
  • 4
  • 37
  • 40
33
votes
4 answers

How to recognize migration direction (up or down) with Rails 3 style migrations (def change)?

I really like the Rails 3 style migrations, i.e. one change method being smart enough to recognize if the migrations is being installed or rolled back, so I don't have to write up and down methods mirroring each other. But I have situation that I…
szeryf
  • 3,197
  • 3
  • 27
  • 28
33
votes
4 answers

Running migrations with Rails in a Docker container with multiple container instances

I've seen lots of examples of making Docker containers for Rails applications. Typically they run a rails server and have a CMD that runs migrations/setup then brings up the Rails server. If I'm spawning 5 of these containers at the same time, how…
32
votes
6 answers

Rails Migrations: tried to change the type of column from string to integer

I created a table in my rails app with rails generate migrations command. Here is that migration file: class CreateListings < ActiveRecord::Migration def change create_table :listings do |t| t.string :name t.string :telephone …
banditKing
  • 9,405
  • 28
  • 100
  • 157
31
votes
5 answers

Rails rake db:migrate has no effect

I made a new Rails 3 app today, added a simple migration, and for some reason, nothing happens when I do rake db:migrate. It simply pauses a few seconds, then returns to the command prompt, with no errors or anything. Schema.rb and the database stay…
1 2
3
68 69