简体   繁体   English

Rails迁移已删除关联表中的条目

[英]Rails migration deleted entries from associated tables

In rails (2.3.5) I have 2 associated models: 在rails(2.3.5)中,我有2个关联的模型:

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. 此迁移将删除我的studies表中的所有条目。

My assumption is that the ALTER TABLE command invoked by the add_column deletes the reports table and recreate it with the new column. 我的假设是,由add_column调用的ALTER TABLE命令将删除reports表并使用新列重新创建它。 Because of the association between the Study and Report models, the dependent studies are destroyed. 由于StudyReport模型之间的关联,从属studies被销毁。

If this is true - is there a way to prevent Rails from doing that? 如果是这样-是否有办法防止Rails这样做? Or, can someone explain why this is happening? 或者,有人可以解释为什么会这样吗?

您的模型很好,迁移对我来说很好,所以我最好的猜测是您没有使用正确的命令...您是在使用普通的旧rake db:migrate还是更高级的工具?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM