mongodb/mongoid · error · Mongoid::Errors::DeleteRestriction

Cannot destroy %{document} because of dependent '%{relation}

Error message

Cannot destroy %{document} because of dependent '%{relation}'.

What it means

Error "Cannot destroy %{document} because of dependent '%{relation}'." thrown in mongodb/mongoid.

Source

Thrown at lib/mongoid/association/depending.rb:127

        return unless relation = without_autobuild { send(association.name) }

        if relation.is_a?(Enumerable)
          relation.entries
          relation.each { |doc| doc.destroy }
        else
          relation.destroy
        end
      end

      def _dependent_nullify!(association)
        return unless relation = without_autobuild { send(association.name) }

        relation.nullify
      end

      def _dependent_restrict_with_exception!(association)
        if (relation = without_autobuild { send(association.name) }) && !relation.blank?
          raise Errors::DeleteRestriction.new(relation, association.name)
        end
      end

      def _dependent_restrict_with_error!(association)
        return unless (relation = without_autobuild { send(association.name) }) && !relation.blank?

        errors.add(association.name, :destroy_restrict_with_error_dependencies_exist)
        throw(:abort, false)
      end
    end
  end
end

View on GitHub (pinned to 0da0e23d71)

When it happens

Trigger: Thrown at lib/mongoid/association/depending.rb:127 when the library encounters an invalid state.

Common situations: See trigger scenarios.


AI-assisted analysis of mongodb/mongoid@0da0e23d71 (2026-08-23). Data as JSON: /api/errors/92215edb43b6c47d. Report an issue: GitHub.