Skip to content

delete_records nullifies PolymorphicOneToMany FKs before destroy, failing when FK is NOT NULL #289

@apacosta

Description

@apacosta

Description

We've been following the great work on polymorphic association fixes (related to #205 and #279) and wanted to flag another edge case we've run into.

We have a polymorphic association between models for "achievement evidence".

Simplified data structure:
Feedback → AchievementEvidence

AchievementEvidence:

  • evidence_id and evidence_type stores the polymorphic parent (e.g. Feedback)
  • achievement_progress_id for the progress record it is attached to

Environment

  • forest_admin_rails 1.26.3
  • Rails 7.x, Ruby 3.x, PostgreSQL

Reproduction

Schema:

create_table "achievement_evidences" do |t|
  t.string "evidence_type", null: false
  t.bigint "evidence_id",   null: false
  t.bigint "achievement_progress_id", null: false
end

Models:

class Feedback < ApplicationRecord
  has_many :achievement_evidences, as: :evidence, dependent: :destroy
end

class AchievementEvidence < ApplicationRecord
  belongs_to :evidence, polymorphic: true
end

Expected

Deleting a Feedback record via the ForestAdmin UI should destroy associated AchievementEvidence records. From the Rails console this works correctly:

Feedback.find(1).destroy
# DELETE FROM "achievement_evidences" WHERE "achievement_evidences"."evidence_type" = 'Feedback' AND "achievement_evidences"."evidence_id" = 1
# DELETE FROM "feedbacks" WHERE "feedbacks"."id" = 1

Actual

Deleting via the ForestAdmin UI fails with:

An error occurred when trying to delete feedback: Your data seems to be invalid:
Validation failed: Evidence is required, Evidence type is not included in the list.

From the logs, ForestAdmin is issuing an UPDATE to nullify the FK before calling destroy:

UPDATE "achievement_evidences" SET "evidence_id" = NULL, "evidence_type" = NULL
WHERE "achievement_evidences"."evidence_type" = 'Feedback' AND "achievement_evidences"."evidence_id" = 1

Since evidence_id is NOT NULL in our schema, this update fails validation before the destroy ever runs.

Happy to provide any additional details or help test a fix. Thanks for your time!

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions