instructure/canvas-lms · error · InvalidDataError

Cannot import to other courses

Error message

Cannot import to other courses

What it means

When a group specifies course_id, the import context must be an Account; the error is raised if context is a Course. A course-level import cannot re-target a different course via course_id — that would let one course write into another — so this guard enforces that course targeting only happens from the account level.

Solutions

  1. Remove course_id from group objects when importing with a Course context (the group lands in the context course).
  2. Run the import with an Account context if you need to target specific courses via course_id.
  3. Split the file: account-context rows with course_id, and course-context rows without.

Example fix

// before
importer = build_importer(context: course); import({object_type: 'group', course_id: 42, ...})
// after
importer = build_importer(context: account); import({object_type: 'group', course_id: 42, ...})
Defensive patterns

Strategy: validation

Validate before calling

raise ArgumentError, "course_id requires an Account context" if group[:course_id].present? && !account.is_a?(Account)

Try / catch

begin
  importer.import_group(group)
rescue Outcomes::Import::InvalidDataError => e
  retry_with_account_context(group) if group[:course_id].present?
end

Prevention

When it happens

Trigger: Calling import_group with group[:course_id] set while the importer was constructed with a Course context (e.g. Outcomes::Import included with course context and rows carrying course_id). Any present course_id on the row triggers the check.

Common situations: Migrating data at course level while reusing an account-level CSV that includes a course_id column; scripting a bulk import with a course context copied from an account-context example.

Understand the failure class

Background: "Must be a positive integer", "Invalid value", "Unsupported": the invalid-argument-value error family, when a library rejects the value you pass — this error's family across 35 libraries.

Related errors


AI-assisted analysis of instructure/canvas-lms@1c9f0bb801 (2026-09-15). Data as JSON: /api/errors/0c02bc9b3ec6e121. Report an issue: GitHub.

Appendix: source

Thrown at lib/outcomes/import.rb:86

        )
      end
    end

    def import_group(group)
      invalid = group.keys.select do |k|
        group[k].present? && OBJECT_ONLY_FIELDS.include?(k)
      end
      if invalid.present?
        raise InvalidDataError, I18n.t(
          "Invalid fields for a group: %{invalid}",
          invalid: invalid.map(&:to_s).inspect
        )
      end

      group_context = context

      if group[:course_id].present?
        raise InvalidDataError, I18n.t("Cannot import to other courses") unless context.is_a?(Account)

        group_context = Course.find_by(id: group[:course_id])

        if group_context.nil?
          raise InvalidDataError, I18n.t(
            "Course with canvas id %{id} not found",
            id: group[:course_id]
          )
        end

        unless child_context?(group_context)
          raise InvalidDataError, I18n.t(
            "Target course %{course_id} is not a child of current account (%{name})",
            course_id: group[:course_id],
            name: context.name
          )
        end
      end

View on GitHub (pinned to 1c9f0bb801)