【问题标题】:Rails 4 dependant destroy through nested relationshipsRails 4 依赖通过嵌套关系销毁
【发布时间】:2016-02-12 12:21:38
【问题描述】:

我正在尝试运行一个脚本来从我们的系统中删除一大群学生,我依靠 rails dependent: :destroy 约定来确保我清理了与这些学生相关的所有数据。

我对这个系统很陌生,但这就是他们在属于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.rb student_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.rb user_application_status 模型

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。关于可能导致此问题的任何想法?

【问题讨论】:

  • 你能在用户模型中显示关系设置吗?
  • 您找到解决方案或原因了吗?

标签: ruby-on-rails ruby postgresql ruby-on-rails-4


【解决方案1】:

据我所见……

# 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 模型上为payments(或payment_tasks)设置一个dependent: :destroy。这将确保付款也被删除。

但是,如果您不希望出现这种情况...那么选项 2:

2) 在student_application 模型上为payments 设置dependent: :nullify。这将确保 ``student_application_idcolumn on the associatedpaymentsobject to the deletedstudent_application` 设置为 null,从而防止上述错误。

:dependent 控制当关联的父对象被销毁时关联对象会发生什么。 dependentcan be found here 的更多选项。

【讨论】:

  • 谢谢老兄,现在让我试试吧,我从没想过尝试无效化,我尝试了第一个选项,但我认为付款模式正在被链条中的其他一些依赖者引用。
  • 所以我尝试了这两个选项,但无论哪种方式都行不通,我已经用模型之间的所有关系更新了故事。
  • 是否需要销毁每个用户上关联的student_applications
  • 我也为学生尝试了:nullifystudent_appliciations,但那是更新记录并且会带来同样的错误。我最好销毁所有相关数据,因为即使不再引用任何人,您仍然可以获得有关用户的大量信息。
  • 所以...您基本上会喜欢销毁用户,并销毁相关的 student_applications,并销毁所有相关的付款...等...等等...?
猜你喜欢
  • 2015-06-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多