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

Why do I need to migrate the test database in Rails?

After creating a new migration file, running the migration, then running my tests I receive: Failure/Error: ActiveRecord::Migration.maintain_test_schema! ActiveRecord::PendingMigrationError: Migrations are pending. To resolve this issue, run: …
ardavis
  • 9,842
  • 12
  • 58
  • 112
17
votes
4 answers

How to create a column of type tinyint(2) or tinyint(3) in Ruby on Rails?

In Ruby on Rails, the following code in a migration creates a column of type tinyint(4) in MySQL: create_table :great_table do |t| t.integer :step_position, :limit => 1 #tinyint end How would I create a column of type tinyint(2) or tinyint(3)?
maxedison
  • 17,243
  • 14
  • 67
  • 114
17
votes
3 answers

rails g migration "command" to generate column rename migration?

I want to auto generate a migration file that looks like this: class RenameDatabaseColumn < ActiveRecord::Migration def change rename_column :events, :subcategory, :subcategory_id end end Is there some way to format my rails g migration…
Dan Baker
  • 1,757
  • 3
  • 21
  • 36
17
votes
3 answers

Rails migration change sequence or order

I wrote a few migrations for my Rails 3 app, but I would like to change the order of the migrations. How can I change the migration order or sequence? Is it as simple as renaming the migration file with what appears to be the timestamp? I know this…
Ryan
  • 10,798
  • 11
  • 46
  • 60
16
votes
3 answers

Are Doctrine migrations usable in production applications?

As previously discussed, we are developing a PHP application around Zend Framework that needs to have it's database upgraded quite frequently and in a cross-database way as we move through development stages. We are currently using Rails Migrations…
GomoX
  • 919
  • 8
  • 21
16
votes
4 answers

How to reset auto increment field in a ActiveRecord migration?

In my migration I have: def up MyModel.destroy_all MyModel.create!({:id=>1,:name=>'foo'}) MyModel.create!({:id=>2,:name=>'fooBar'}) MyModel.create!({:id=>3,:name=>'fooNull'}) end because I need to override data that was already on…
Mr_Nizzle
  • 6,644
  • 12
  • 55
  • 85
16
votes
1 answer

Change foreign key column name in rails

I have a Project migration class like this: class CreateProjects < ActiveRecord::Migration def change create_table :projects do |t| t.string :title t.text :description t.boolean :public t.references :user, index: true, foreign_key:…
Azam Ikram
  • 185
  • 1
  • 1
  • 10
16
votes
3 answers

Rails 4 Migration | Add Table with Reference

I am attempting to create a Collaboration table in my Rails 4 project, but I've run into an issue. I wish it to belong_to a single user, the collaborator. I ran the following command to generate the model and the migration, which I've also copied…
16
votes
3 answers

schema.rb messed up due to migrations in other branches

Currently I'm working with a huge rails application and multiple branches with each a new feature for this application. It happens a lot a feature will require migrations, which shouldn't be a problem until you merge it with master: schema.rb got…
Vikko
  • 1,396
  • 10
  • 23
16
votes
2 answers

Setting up Discourse on Ubuntu 12.04 LTS

I have setup a new database for installing Discourse in PostgreSQL. When I run rake db:migrate, it creates most of the tables, but it then fails: -- execute("INSERT INTO archetypes (name_key, created_at, updated_at) VALUES ('poll',…
15
votes
5 answers

Rails: how to rollback a botched migration

I'm an idiot...screwed up a migration in Rails: thinking migrations would work like model generators (using references:modelname) I did the following: $ rails g migration add_event_to_photos references:event which created the migration class…
Meltemi
  • 37,979
  • 50
  • 195
  • 293
15
votes
1 answer

Rails migration, references `unique: true` isn't generating `unique: true` in schema causing constancy_fail check to fail

I have the following migration class CreateBooking < ActiveRecord::Migration[5.1] def change create_table :bookings do |t| t.integer :day_period, default: 0 t.references :service, foreign_key: true, unique: true, dependent:…
Thermatix
  • 2,757
  • 21
  • 51
15
votes
1 answer

Deleting table from schema - Rails

I want to delete a table in my schema. I created the database when I first started the project and want the table removed. What is the best way of doing this? I tried rails g migration drop table :installs but that just creates a empty…
Bitwise
  • 8,021
  • 22
  • 70
  • 161
15
votes
1 answer

Creating model and index through one-line command?

I know I can easily create a model with this one line. Now suppose I want to add an index on username. How can I do so with one line without going to edit the migration file manually? script/rails generate model TwitterUser username:string…
Henley
  • 21,258
  • 32
  • 119
  • 207
14
votes
2 answers

Updating migration timestamps in feature branches

Let's say there's active development in both my main branch (devlop) and my feature branch. Both are adding migrations now and again. Before merging the feature branch into the main branch, I'm going to rebase it onto the main branch. So it only…
John Bachir
  • 22,495
  • 29
  • 154
  • 227