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
31
votes
8 answers

Rails migration does not change schema.rb

I have a rails migration that is not being applied to my schema.rb. The migration should create a table: class CreateUserGraphs < ActiveRecord::Migration def change create_table :user_graphs do |t| t.string :name t.string :content …
Don P
  • 60,113
  • 114
  • 300
  • 432
30
votes
3 answers

Destroy/Remove database in Rails

Is it possible to completely remove the database and all migration records etc from an existing application so I can redesign the database from scratch?
26
votes
4 answers

how to set default value to column in rails while creating migration

I am new to Model in rails. I know how to create model & how to add column to them. Now I want to set default value to a column but I am not getting that how exactly I can do it. I generated new model rails g model User then added column to…
mandar.gokhale
  • 1,876
  • 1
  • 18
  • 37
26
votes
3 answers

Managing mongoid migrations

Can someone give me a short introduction to doing DB migrations in Rails using Mongoid? I'm particularly interested in lazy per document migrations. By this, I mean that whenever you read a document from the database, you migrate it to its latest…
Paul Biggar
  • 27,579
  • 21
  • 99
  • 152
26
votes
2 answers

index: true vs foreign_key: true (Rails)

Following a guide, I ran the following command: rails g migration CreateSnippetsUsers snippet:belongs_to user:belongs_to This created the following migration: class CreateSnippetsUsers < ActiveRecord::Migration[5.0] def change create_table…
Mirror318
  • 11,875
  • 14
  • 64
  • 106
26
votes
2 answers

Merging ActiveRecord migrations out of order

Let's say I'm working in git on my Rails app, and I have two branches, each with their own migration. 1) branch001 creates a table called tableA via migration 20160101000000_create_table_A 2) branch002 creates a table called tableB via migration…
user2490003
  • 10,706
  • 17
  • 79
  • 155
26
votes
2 answers

How to add foreign key in rails migration with different table name

How can I assign different table name with adding foreign key. for e.g I have a model like class MyPost < ActiveRecord::Base has_many :comments, class_name: PostComment end class PostComment < ActiveRecord::Base belongs_to :post, class_name:…
25
votes
2 answers

Migration with a decimal number with 2 trailing digits

Rails 3.2 MySQL gem I have the following in my migration: t.decimal :pre_tax_total, default: nil, scale: 2 t.decimal :post_tax_total, default: nil, scale: 2 Based on what I read, scale:2 will produce a decimal with 2 trailing digits. When I run…
EastsideDev
  • 6,257
  • 9
  • 59
  • 116
25
votes
4 answers

Specify custom index name when using add_reference

I have the following migration class LinkDoctorsAndSpecializations < ActiveRecord::Migration def up add_reference :doctors, :doctor_specialization, polymorphic: true, index: true end def down remove_reference :doctors,…
bhanu
  • 2,260
  • 3
  • 24
  • 35
25
votes
2 answers

Rails ActiveRecord::Migration what is the difference between index: true and add_index?

What is the difference between t.boolean :is_live, index: true and add_index :table_name, :is_live If there is no difference, how come only the add_index is reflected in schema.rb. When I use index: true, I can't actually see the index in…
samol
  • 18,950
  • 32
  • 88
  • 127
24
votes
2 answers

Generate an auto increment field in rails

I have a model Token, which has a field token_number that I need to auto increment (starting from 1001), if and only if the user does not provide it. The problem is that, since the user has the option to provide this field, I can't exactly query the…
Wahaj Ali
  • 4,093
  • 3
  • 23
  • 35
24
votes
1 answer

Rails 3.1: can't write to column in same migration that adds it

I had an add_column migration that would run fine. However, after running it and firing up a console, I would find the first_name and last_name columns completely empty. I tried using save! instead and it had the same effect--no errors reported. …
24
votes
1 answer

Meaning of "Expected string default value for ..." on Ruby on Rails

Recently I've created an app for Ruby (2.3.3) on Rails (5.0.0.1): $ rails _5.0.0.1_ new myapp --database=postgresql -T After setting up the Gemfile and testing the connectivity to my databases: $ rails db:migrate I've tried to generate models but…
23
votes
4 answers

Rails + Postgres migration - why am I receiving the error "PG::UndefinedFunction: ERROR: function gen_random_uuid() does not exist"?

One of my Rails migrations uses a uuid as the primary key. The Postgres extension gen_random_uuid() should solve this issue, but I continue to get the error after installing the relevant extension (uuid-ossp).
user3006381
  • 2,735
  • 3
  • 23
  • 32
23
votes
1 answer

What is a "circular argument reference" error, with activesupport time_zone?

I'm new in ruby on rails, and I am trying to create a tutorial. I have a problem when I execute rake db:migrate. hugo@ubuntu:~/pin_board$ rake…
Francisco Possetto
  • 443
  • 1
  • 3
  • 10