2016-02-12 64 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'? –