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
722
votes
24 answers

How to drop columns using Rails migration

What's the syntax for dropping a database table column through a Rails migration?
Ethan
  • 9,558
  • 5
  • 27
  • 24
368
votes
8 answers

Add a reference column migration in Rails 4

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? Here is what I have. I'm not sure if I should use (1) :user_id, :int or (2) :user, :references. I'm not even sure if…
202
votes
5 answers

Check if a table exists in Rails

I have a rake task that won't work unless a table exists. I'm working with more than 20 engineers on a website so I want to make sure they have migrated the table before they can do a rake task which will populate that respective table. Does AR have…
thenengah
  • 42,557
  • 33
  • 113
  • 157
196
votes
24 answers

Add timestamps to an existing table

I need to add timestamps (created_at & updated_at) to an existing table. I tried the following code but it didn't work. class AddTimestampsToUser < ActiveRecord::Migration def change_table add_timestamps(:users) end end
leonel
  • 10,106
  • 21
  • 85
  • 129
191
votes
4 answers

Rails migrations: Undo default setting for a column

I have the problem, that I have an migration in Rails that sets up a default setting for a column, like this example: def self.up add_column :column_name, :bought_at, :datetime, :default => Time.now end Suppose, I like to drop that default…
wulfovitch
  • 3,216
  • 2
  • 24
  • 15
191
votes
10 answers

Show pending migrations in rails

Is there a rake task that shows the pending migrations in a rails app?
readonly
  • 343,444
  • 107
  • 203
  • 205
157
votes
6 answers

Specifying column name in a "references" migration

I want to make a migration in Rails, referencing another table. Usually, I would do something like: add_column :post, :user, :references This creates a column named user_id in posts table. But what if, instead of user_id, I want something like…
caarlos0
  • 20,020
  • 27
  • 85
  • 160
148
votes
1 answer

What is the difference between t.belongs_to and t.references in rails?

What is the difference between t.references and t.belongs_to? Why are we having those two different words? It seems to me they do the same thing? Tried some Google search, but find no explanation. class CreateFoos < ActiveRecord::Migration def…
Tornskaden
  • 2,203
  • 2
  • 15
  • 12
148
votes
4 answers

how to generate migration to make references polymorphic

I have a Products table and want to add a column: t.references :imageable, :polymorphic => true I was trying to generate migration for this by doing: $ rails generate migration AddImageableToProducts imageable:references:polymorphic but I am…
railslearner
  • 1,761
  • 2
  • 14
  • 18
91
votes
3 answers

Rails migrations: self.up and self.down versus change

Looks like the new rails version has "change" versus self.up and self.down methods. So what happens when one has to roll back a migration how does it know what actions to perform. I have the following method that I need to implement based on an…
banditKing
  • 9,405
  • 28
  • 100
  • 157
84
votes
9 answers

Rolling back a failed Rails migration

How do you roll back a failed rails migration? I would expect that rake db:rollback would undo the failed migration, but no, it rolls back the previous migration (the failed migration minus one). And rake db:migrate:down VERSION=myfailedmigration…
insane.dreamer
  • 2,052
  • 1
  • 17
  • 21
84
votes
7 answers

Rails Migration: add_reference to Table but Different Column Name For Foreign Key Than Rails Convention

I have the following two Models: class Store < ActiveRecord::Base belongs_to :person end class Person < ActiveRecord::Base has_one :store end Here is the issue: I am trying to create a migration to create the foreign key within the people…
Neil
  • 4,578
  • 14
  • 70
  • 155
75
votes
4 answers

Where is the documentation page for ActiveRecord data types?

I can't find the active record documenation page that has a list of all the data types. Can someone help me out?
Blankman
  • 259,732
  • 324
  • 769
  • 1,199
71
votes
6 answers

has_many, belongs_to relation in active record migration rails 4

I have a User model and a Task model. I have not mentioned any relation between them while creating them. I need to establish that User has_many Tasks and a Task belongs_to User through a migration What would be the migration generation command…
70
votes
7 answers

Why am I asked to run 'rake db:migrate RAILS_ENV=test'?

On Rails 4.0.0.rc1, Ruby 2.0.0, after I run a migration, I see the following error when I try to run a test through rspec: /Users/peeja/.rbenv/versions/2.0.0-p0/lib/ruby/gems/2.0.0/gems/activerecord-4.0.0.rc1/lib/active_record/migration.rb:376:in …
Peeja
  • 13,683
  • 11
  • 58
  • 77
1
2 3
68 69