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
14
votes
3 answers

Rails: Remove foreign key constraint

I have following association with user and semester. And I created user table with semester id as foreign key. So user is not created if semester id not present. But semester id is optional in the registration form. class User < ApplicationRecord …
user1670773
  • 967
  • 4
  • 12
  • 23
14
votes
3 answers

Migrating DATA - not just schema, Rails

Sometimes, data migrations are required. As time passes, code changes and migrations using your domain model are no longer valid and migrations fail. What are the best practices for migrating data? I tried make an example to clarify the…
oma
  • 38,642
  • 11
  • 71
  • 99
14
votes
2 answers

Rails 3.2.6 & database views creation through migrations

I'm using rails 3.2.6 and I need to create a database VIEW. As usual I created a migration and I tried to achieve the goal using the execute method. Unfortunately the migration generates a table, not a view. Why? Many thanks in…
Mauro Nidola
  • 468
  • 4
  • 14
13
votes
4 answers

What's a good way to clean up my migrations in Rails?

So I've been working on this web app for a year now and I would like to compile to schema into ONE migration, that way my text editor loads faster, git working directory isn't so cluttered. Search find will be faster. Any my config/db won't be…
13
votes
2 answers

COLLATE=utf8_unicode_ci getting removed from schema.rb after migrate

Running rails 5.0.2 The tables in our schema.rb in source control seem to mostly have the format: create_table "app_files", force: :cascade, options: "ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci" do |t| Note the…
Andrew
  • 942
  • 10
  • 26
13
votes
6 answers

Couldn't run migration after spring update in Rails

I am facing a error when I run any migration as: raj@notebook-pc:~/Desktop/Projects/invoicemanagement$ rails g migration RemoveDescriptionOfGoodsFromInvoiceDetails description_of_goods:string Warning: You're using Rubygems 1.8.23 with Spring.…
sachin god
  • 685
  • 2
  • 12
  • 29
13
votes
2 answers

Rails: "t.references" not working when creating index

class CreateBallots < ActiveRecord::Migration def change create_table :ballots do |t| t.references :user t.references :score t.references :election t.string :key t.timestamps end add_index :ballots, :user …
Muhd
  • 24,305
  • 22
  • 61
  • 78
13
votes
2 answers

How to change primary key in rails migration file?

I need to migrate an old mysql table like this: Products name (string, primary_key) to this schema: Products id (integer, primary_key, auto_generated) name (unique) I need the Products.id values populated in the new table. How can i write…
Wint
  • 2,268
  • 2
  • 22
  • 32
13
votes
3 answers

Ruby on Rails: how to migrate changes made on models?

In a Rails application, how can I migrate the changes I make in models? For instance, I know that if I create a model with command rails g model Person name:string, a migration will be created as well. However, if after this step I go to the created…
Rui
  • 5,900
  • 10
  • 38
  • 56
13
votes
3 answers

Rails Migration for ID Column to Start at 1,000 and Autoincrement Up From There?

I'd like the ID's of my Order model to start at 1000, and count up autoincrementally from there. Can this be done via migration?
Jordan Warbelow-Feldstein
  • 10,510
  • 12
  • 48
  • 79
12
votes
3 answers

How to add a new column in an existing table in Rails 5?

I want to add a new column in one of my table in Rails 5. I recently renamed a column by using the following way: rails g migration ChangeJobsTable then in 20160802104312_change_jobs_table.rb: class ChangeJobsTable < ActiveRecord::Migration[5.0] …
Amrinder Singh
  • 5,300
  • 12
  • 46
  • 88
12
votes
4 answers

add a database column with Rails migration and populate it based on another column

I'm writing a migration to add a column to a table. The value of the column is dependent on the value of two more existing columns. What is the best/fastest way to do this? Currently I have this but not sure if it's the best way since the groups…
11
votes
2 answers

Generate Rails migrations from a schema

I am creating a new Rails application which will work with an existing schema. I have been given the schema SQL but I want to create Rails migrations to populate the database in development. The schema is not overly complicated, with around 20…
AaronThomson
  • 763
  • 8
  • 19
11
votes
3 answers

Weird Rails migration / schema.rb issue

A while back I ran the following migration: class CreatePipelineSpecs < ActiveRecord::Migration def change create_table :pipeline_specs do |t| t.integer :id_no t.string :od t.string :wt t.string :material t.string…
Dan Tappin
  • 2,692
  • 3
  • 37
  • 77
11
votes
3 answers

Rails migration to change column type from text to json (Postgresql)

I've been trying unsuccessfully to change a column type in my Postgres database from text to json. Here's what I've tried... class ChangeNotesTypeInPlaces < ActiveRecord::Migration[5.0] def up execute 'ALTER TABLE places ALTER COLUMN notes…
soultrust
  • 581
  • 1
  • 7
  • 17