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

Is there a way to resolve Rails deleted migrations?

I happened to have deleted migrations and I don't want to revert these removed migrations. This is what rake db:migrate:status produces: Status Migration ID Migration Name ------------------------------------------------------ up 0 …
11
votes
4 answers

Rails: PG::UndefinedTable: ERROR: relation "..." does not exist

On migration I get the following error message: PG::UndefinedTable: ERROR: relation "actioncodes" does not exist : ALTER TABLE "organizations" ADD CONSTRAINT "fk_rails_4ecaa2493e" FOREIGN KEY ("actioncode_id") REFERENCES "actioncodes" ("id") I…
Nick
  • 3,496
  • 7
  • 42
  • 96
11
votes
3 answers

Rails 4 Migration: Mysql2::Error: Data too long for column 'xxxx'

Here is my schema.rb create_table "users", force: true do |t| t.string "name", limit: 6 t.string "email" t.datetime "created_at" t.datetime "updated_at" end I set the limit fo string for the column "name". Then, in…
tzzzoz
  • 336
  • 1
  • 4
  • 12
11
votes
4 answers

Rails 3 migration for adding unsigned int column

I would like to generate a migration to add a column to a table which has a data type of unsigned int. I wish to use it to store IP addresses as mentioned here in this article. I came across this question but it will make the migration database…
Shobhit
  • 647
  • 1
  • 7
  • 19
10
votes
3 answers

Issue with adding a not null columns to an existing table

I have problem with adding not nullable columns to a table. I read many posts about this and it should be correct. Migration code: def change add_column :individual_trainings, :start_on, :time add_column :individual_trainings, :end_on,…
Prezes Łukasz
  • 938
  • 1
  • 9
  • 30
10
votes
2 answers

Rails 4 migration to change columns data type from string to integer and back preserving data (postgres)

I need to convert string fields into integer and use enum instead. What is the best way to do this without losing data? This is current migration: class CreateSystems < ActiveRecord::Migration def change create_table :systems do |t| …
Pav31
  • 540
  • 7
  • 19
10
votes
4 answers

Rails4 migration- adding null: false to an add_reference method?

I've just started learning rails so sorry if the answer to this if fairly obvious. I've added migrations for posts and categories tables in my app and am now adding a reference to categories in my posts table with a default value of not null using…
L457
  • 1,002
  • 1
  • 13
  • 33
10
votes
1 answer

Use Postgres Multiple Schema Database in Rails

I'm developing a multitenant app following this article. The problem is when I run all migrations the first time. In schema.rb file are only the tables for the public schema but what happend with the others schemas? and how can I create others…
9
votes
2 answers

How to stop Rails 3.1 migration from running in a transaction?

I want to add an index to a production database. Fortunately we're running Postgres, which nicely allows concurrent indexing, so we can add an index without downtime. The catch -- concurrent indexes cannot be added from within a transaction, and…
jpadvo
  • 6,031
  • 3
  • 26
  • 30
9
votes
5 answers

Rails model generation fails due to foreign key type

I created a model with the following command: rails g model UserCertification user:references certification:references certification_no:string which is referencing to my devise user model user:references. On the db:migrate I receive the following…
Christian K.
  • 107
  • 1
  • 5
9
votes
1 answer

Rails 4: How to drop or remove (join) tables tables from database?

In the Rails 4 documentation (http://guides.rubyonrails.org/migrations.html#creating-a-join-table) I see that to drop a table or a join table, one can use the following methods in command line: drop_table and drop_join_table. However, it further…
robskrob
  • 2,720
  • 4
  • 31
  • 58
9
votes
2 answers

rails changes schema.rb for no reason

Everytime I run a rake db:migrate rails decides to change my schema.rb file. In some cases this is completely reasonable, however in some other cases it seems that it's doing it for no reason. The case I'm confused about is when I pull a new…
wesdotcool
  • 475
  • 4
  • 14
9
votes
3 answers

Rails creating schema_migrations - Mysql2::Error: Specified key was too long

I am using Rails 3.2.6 and Mysql 6.0.9 (but I have exactly the same error on MySQL 5.2.25) When I create new database (rake db:create) and then when I try to load the schema (rake schema:load) I get this error: Mysql2::Error: Specified key was too…
Lukáš Voda
  • 1,262
  • 1
  • 13
  • 13
8
votes
1 answer

heroku error when i running migration (rails 3.1 cedar stack)

SOLVED: was due to network limitation I just pushed my app and i need to run my migration files to initialize the database i get the following error: what should i do ? EDIT: heroku run bash -app appname yields the same error Running rake db:migrate…
8
votes
1 answer

Increase statement timeout for rails migration

My app rails app has a postgres statement timeout set to 5 seconds in production. This timeout works well for us, helps us catch errors in production, kill bad queries, etc. However, when we are deploying a change we sometimes want to run data…
aoh
  • 1,090
  • 2
  • 13
  • 25