我想运行一个脚本从我们的系统中删除一大堆学生,我依靠轨道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