2016-02-12 62 views
0

我想运行一个脚本从我们的系统中删除一大堆学生,我依靠轨道dependent: :destroy约定来确保清理与这些学生相关的所有数据。Rails 4依赖通过嵌套关系销毁

我对这个系统很新,但这是他们如何在属于studentstudent_application模型中构建has_many关系。

student.rb学生模型

has_many :applications, class_name: "StudentApplication", dependent: :destroy 
has_many :season_classes, through: :applications 
has_many :payments, foreign_key: "student_id", dependent: :destroy 

student_application.rbstudent_application模型

belongs_to :student, touch: true 
has_many :user_application_statuses, -> { order(id: :asc) }, dependent: :destroy 
has_many :user_application_tasks, through: :user_application_statuses 
has_many :file_upload_tasks, through: :user_application_statuses, class_name: "Tasks::FileUploadTask", source: :user_application_tasks 
has_many :payment_tasks, through: :user_application_statuses, class_name: "Tasks::PaymentTask", source: :user_application_tasks 
has_many :payments, through: :payment_tasks 

user_application_status.rbuser_applicaton_statu S模式

belongs_to :application_status 

# Student links 
belongs_to :student_application 
has_one :student, through: :student_application 

payment.rb支付模式

belongs_to :student 
has_one :payment_task, class_name: "Tasks::PaymentTask" 
has_many :transactions 

当我删除我得到这个错误

PG::ForeignKeyViolation: ERROR: update or delete on table "student_applications" violates foreign key constraint "payments_student_application_id_fkey" on table "payments" 
DETAIL: Key (id)=(24747) is still referenced from table "payments". 
: DELETE FROM "student_applications" WHERE "student_applications"."id" = $1 
    (0.3ms) ROLLBACK 
ActiveRecord::InvalidForeignKey: PG::ForeignKeyViolation: ERROR: update or delete on table "student_applications" violates foreign key constraint "payments_student_application_id_fkey" on table "payments" 
DETAIL: Key (id)=(24747) is still referenced from table "payments". 
: DELETE FROM "student_applications" WHERE "student_applications"."id" = $1 

用户起初我以为,有一个更深层次关系层面的对象被遗漏。但据我可以从看表和源代码,告诉没有payments_student_application_id_fkey参考代码的任何地方,但我已经在structure.sql文件中找到这个

-- 
-- Name: payments_student_application_id_fkey; Type: FK CONSTRAINT; Schema: public; Owner: - 
-- 

ALTER TABLE ONLY payments 
    ADD CONSTRAINT payments_student_application_id_fkey FOREIGN KEY (student_application_id) REFERENCES student_applications(id); 

我们使用Rails 4.1.14.1及Ruby 2.1.6和Postgres for db。任何想法可能会导致这个问题?

+0

你能在用户模型中显示关系设置吗? –

+0

你有没有找到解决方案或原因? – Mirv

回答

2

从我所看到的...

# in pseudocodes 
user has_one or has_many student_application dependent destroy 
student_application belongs to a user 
student_application has_many payments 

因此删除用户会导致关联student_application遭到删除,以及...但student_application的ID被从支付表中引用,从而导致错误。

两个就绪型解决方案,我可以看到:

1)在student_application模型设置一个dependent: :destroypayments(或payment_tasks)为好。这将确保付款也被删除。

但是,如果你不希望这是情况......然后选择2:

2)把student_application模型dependent: :nullifypayments。这将确保`student_application_id column on the associated付款object to the deleted student_application`设置为空,防止上述错误。

:dependent控制关联父对象销毁时发生的情况。更多选项dependentcan be found here

+0

感谢bud,让我现在尝试一下,我从来没有想过尝试使用nullify,我尝试了第一个选项,但是我认为付款模型后来在链中被其他一些依赖项引用。 – TheLegend

+0

所以我尝试了两种选择,它不会以任何方式工作,我已经用模型之间的所有关系更新了故事。 – TheLegend

+0

你是否需要销毁每个用户的关联'student_applications'? –