我想運行一個腳本從我們的系統中刪除一大堆學生,我依靠軌道dependent: :destroy
約定來確保清理與這些學生相關的所有數據。Rails 4依賴通過嵌套關係銷燬
我對這個系統很新,但這是他們如何在屬於student
的student_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。任何想法可能會導致這個問題?
你能在用戶模型中顯示關係設置嗎? –
你有沒有找到解決方案或原因? – Mirv