Rails migration deleted entries from associated tables

66 views Asked by At

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 studiesare destroyed.

If this is true - is there a way to prevent Rails from doing that? Or, can someone explain why this is happening?

1

There are 1 answers

1
msanteler On

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?