In rails (2.3.5) I have 2 associated models:
class Report < ActiveRecord::Base
has_many :studys, :dependent => :destroy
end
class Study < ActiveRecord::Base
validates_presence_of :name, :report_id
belongs_to :report
end
I'm running a migration on the reports table:
class AddReportUsername< ActiveRecord::Migration
def self.up
add_column :reports, :username, :text, :limit => 20
end
This migration deletes all entries in my studies
table.
My assumption is that the ALTER TABLE
command invoked by the add_column
deletes the reports
table and recreate it with the new column. Because of the association between the Study
and Report
models, the dependent studies
are destroyed.
If this is true - is there a way to prevent Rails from doing that? Or, can someone explain why this is happening?
You're models and migration look fine to me, so my best guess is that you're not using the right command... Are you just using a plain old
rake db:migrate
or something fancier?