Lighthouse has a new layout. Prefer the old one? Return to the old layout, and switch back any time from the link at the top of each page.

This project is archived and is in readonly mode.

Request for automatic invocation of 'reset_column_information' in Migrations

#6234

Short version: please run Model.reset_column_information after running a Migration.

Long version:
I just spent quite some time hunting for the fact that if you do

class ModifyModel < ActiveRecord::Migration
  def self.up
    add_column :model, :some_column, :string
    p Model.columns
  end
end

the column you just added won't show up and for all practical purposes doesn't exist. You need to issue a

Model.reset_column_information

to get access to the column. This causes problems when, for instance, a rake task is used to recreate a certain default database situation and consists of running migrations and doing seeding afterwards. I'm sure I'm not the only one that ran into this issue and it could be prevented/fixed by always running 'reset_column_information' after a Migration, which sounds very sensible to me. I would say it at least violates the principle of least surprise that adding a column to a Model does not actually result in that column being available.

Reported by Ivo Wever · December 30th, 2010 @ 03:52 PM

State: open
Milestone: none
Assigned to: nobody
Importance: none

Activity

  1. Joshua Pinter
    Joshua Pinter

    I concur. Just ran into this myself.

    Spent a few hours hunting it down and another hour making updates to my migrations.

    January 4th, 2011 @ 07:39 PM

  2. Oscar Del Ben
    Oscar Del Ben

    In the rails test suite, there is a test where they specifically call reset_column_information:

    def test_native_types
          Person.delete_all
          Person.connection.add_column "people", "last_name", :string
          Person.connection.add_column "people", "bio", :text
          Person.connection.add_column "people", "age", :integer
          Person.connection.add_column "people", "height", :float
          Person.connection.add_column "people", "wealth", :decimal, :precision => '30', :scale => '10'
          Person.connection.add_column "people", "birthday", :datetime
          Person.connection.add_column "people", "favorite_day", :date
          Person.connection.add_column "people", "moment_of_truth", :datetime
          Person.connection.add_column "people", "male", :boolean
          Person.reset_column_information
    
          assert_nothing_raised do
            Person.create :first_name => 'bob', :last_name => 'bobsen',
              :bio => "I was born ....", :age => 18, :height => 1.78,
              :wealth => BigDecimal.new("12345678901234567890.0123456789"),
              :birthday => 18.years.ago, :favorite_day => 10.days.ago,
              :moment_of_truth => "1782-10-10 21:40:18", :male => true
          end
    
          # Other stuff
    end
    

    I think this is just the way it's supposed to be. One reason could be that otherwise they'd have to call reset_column_information after every single operation (add_column, remove_column, etc).

    January 5th, 2011 @ 09:17 AM

  3. Ivo Wever
    Ivo Wever

    Well, this feature request is for running the method after every Migration; not within the migration after each Model modification. But even then:

    irb(main):008:0> puts Benchmark.measure {1000000.times {User.reset_column_information;}}
      3.050000   0.010000   3.060000 (  3.079835)
    

    I'm not worried about the performance impact of this, the more so since we are dealing on a relatively infrequent operation as running migrations.

    January 5th, 2011 @ 10:07 AM

  4. rails
    rails
    • State changed from new to open

    This issue has been automatically marked as stale because it has not been commented on for at least three months.

    The resources of the Rails core team are limited, and so we are asking for your help. If you can still reproduce this error on the 3-0-stable branch or on master, please reply with all of the information you have about it and add "[state:open]" to your comment. This will reopen the ticket for review. Likewise, if you feel that this is a very important feature for Rails to include, please reply with your explanation so we can consider it.

    Thank you for all your contributions, and we hope you will understand this step to focus our efforts where they are most helpful.

    April 6th, 2011 @ 01:09 AM

  5. rails
    rails
    • State changed from open to stale

    April 6th, 2011 @ 01:09 AM

  6. learn italian online
    learn italian online
    • State changed from stale to open

    The resources of the Rails core team are Ray Ban 2140 limited, and so we are asking for your help. If you can still
    learn italian online reproduce this error on the 3-0-stable branch or on master, please reply with all of the information you have about it and add "[state:open]" to your comment. This will reopen the ticket for review. learn a language Likewise, if you feel that this is a very important feature for Rails to include, Ray Ban 3025 ple ase reply with your explanation so we can consider it.

    May 20th, 2011 @ 01:20 AM