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
23
votes
4 answers

Rails 4. Migrate table id to UUID

I have a table: db/migrate/20140731201801_create_voc_brands.rb: class CreateVocBrands < ActiveRecord::Migration def change create_table :voc_brands do |t| t.string :name t.timestamps end end end But I need to change table…
Derk153
  • 672
  • 2
  • 9
  • 25
22
votes
3 answers

Delete old migrations files in a Rails app

Is it permissible to delete (or archive) old migration files in a Rails app if the schema is stable? My migrations are numerous and I suspect there may be some problem in there somewhere, because I occasionally have problems migrating the database…
niftygrifty
  • 3,452
  • 2
  • 28
  • 49
21
votes
1 answer

Is ar_internal_metadata table created in production?

I'm wondering if I need to create a migration for creating ar_internal_metadata in production on Rails 5
maschwenk
  • 569
  • 1
  • 9
  • 20
21
votes
3 answers

Rails Migration to convert string to integer using conversion

There is a good question here I want to elaborate on. I am trying to convert a column in my database form a string to an integer. I thought the conversion would be pretty straight forwrad. Currently my strings are ["10", "12", "125", "135", "140",…
Jay Killeen
  • 2,832
  • 6
  • 39
  • 66
21
votes
6 answers

How to add new seed data to existing rails database

I am working on an application that is already deployed to some test and staging systems and various developers workstations. I need to add some additional reference data but i'm not sure how to add it. Most of the advice says use seed.rb, however…
toby
  • 683
  • 2
  • 6
  • 16
21
votes
2 answers

Rails Migration Error w/ Postgres when pushing to Heroku

I'm trying to perform the following up migration to change the column "number" in the "tweet" model's table class ChangeDataTypeForTweetsNumber < ActiveRecord::Migration def up change_column :tweets do |t| t.change :number, :integer …
dougiebuckets
  • 2,383
  • 3
  • 27
  • 37
20
votes
2 answers

What determines if rails includes id: :serial in a table definition?

I'm working with an existing rails app, using postgresql. Its schema.rb file has id: :serial for many, but not all, tables: create_table "foos", id: :serial, force: :cascade do |t| When I run rails db:migrate:reset, id: :serial is removed. We are…
John Bachir
  • 22,495
  • 29
  • 154
  • 227
20
votes
7 answers

Reversible migration for change_column_default from not having any default in Rails

The Rails guides to active record migrations says that you can do change_column_default :products, :approved, from: true, to: false I've got a change method in Rails that's similar to the following: change_column_default :people, :height, from:…
20
votes
2 answers

Rails 4: schema.db shows "Could not dump table "events" because of following NoMethodError# undefined method `[]' for nil:NilClass"

I've been working on a Rails 4.0 application with sqlite (default for Rails development environment) for events (hackathons) which has a parent model, Event, for which there can be many Press_Blurbs. First I ran some scaffolding generators which…
huertanix
  • 761
  • 1
  • 9
  • 22
20
votes
7 answers

Ruby on Rails: adding columns to existing database

I'm getting an error: SQLite3::SQLException: no such column: ideas.list_id: SELECT "ideas".* FROM "ideas" WHERE "ideas"."list_id" = 2 But I added t.integer :list_id to my db migration file: class CreateIdeas < ActiveRecord::Migration def…
hellomello
  • 8,219
  • 39
  • 151
  • 297
19
votes
4 answers

How to add sequences to a migration and use them in a model?

I want to have a "Customer" Model with a normal primary key and another column to store a custom "Customer Number". In addition, I want the db to handle default Customer Numbers. I think, defining a sequence is the best way to do that. I use…
19
votes
6 answers

Add auto increment back to primary key column in Rails

By mistake I removed the autoincrement option from id field of my table. Can anyone tell me how I can reinsert the option of autoincrement back through migration?
Somesh
18
votes
5 answers

Can I add comments to a table or column using ActiveRecord Migrations?

In MySQL (and other SQL databases), it can be helpful to add comments to a table or column whose purpose may be unclear. (Search MySQL's create table syntax for "comment" for examples.) Is there a way to do this in an ActiveRecord Migration? I have…
Nathan Long
  • 122,748
  • 97
  • 336
  • 451
18
votes
5 answers

Heroku run rake db:migrate results in no change in the database, app restarted several times

I have a problem with pushing my migrations to the production database. The issue: I've altered database schema by adding 1 column. I've migrated it to the production database: MacBook-Air-Mac:app msc$ rake db:migrate RAILS_ENV="production"…
rhjs
  • 231
  • 1
  • 3
  • 9
17
votes
1 answer

How to do Rails migration involving Paperclip

How do people write their Rails migrations that involve Paperclip? I feel that I might be missing something obvious as I have now written my own migration helpers hacks that makes it easier and also take care of doing necessary filesystem changes.…
Mattias Wadman
  • 11,172
  • 2
  • 42
  • 57