{"record":{"id":"0c02bc9b3ec6e121","repo":"instructure/canvas-lms","slug":"cannot-import-to-other-courses","errorCode":null,"errorMessage":"Cannot import to other courses","messagePattern":"Cannot import to other courses","errorType":"validation","errorClass":"InvalidDataError","httpStatus":null,"severity":"error","filePath":"lib/outcomes/import.rb","lineNumber":86,"sourceCode":"        )\n      end\n    end\n\n    def import_group(group)\n      invalid = group.keys.select do |k|\n        group[k].present? && OBJECT_ONLY_FIELDS.include?(k)\n      end\n      if invalid.present?\n        raise InvalidDataError, I18n.t(\n          \"Invalid fields for a group: %{invalid}\",\n          invalid: invalid.map(&:to_s).inspect\n        )\n      end\n\n      group_context = context\n\n      if group[:course_id].present?\n        raise InvalidDataError, I18n.t(\"Cannot import to other courses\") unless context.is_a?(Account)\n\n        group_context = Course.find_by(id: group[:course_id])\n\n        if group_context.nil?\n          raise InvalidDataError, I18n.t(\n            \"Course with canvas id %{id} not found\",\n            id: group[:course_id]\n          )\n        end\n\n        unless child_context?(group_context)\n          raise InvalidDataError, I18n.t(\n            \"Target course %{course_id} is not a child of current account (%{name})\",\n            course_id: group[:course_id],\n            name: context.name\n          )\n        end\n      end","sourceCodeStart":68,"sourceCodeEnd":104,"githubUrl":"https://github.com/instructure/canvas-lms/blob/1c9f0bb8013ed69c4f2efe11fd483025469b7e6c/lib/outcomes/import.rb#L68-L104","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Remove course_id from group objects when importing with a Course context (the group lands in the context course).","Run the import with an Account context if you need to target specific courses via course_id.","Split the file: account-context rows with course_id, and course-context rows without."],"exampleFix":"// before\nimporter = build_importer(context: course); import({object_type: 'group', course_id: 42, ...})\n// after\nimporter = build_importer(context: account); import({object_type: 'group', course_id: 42, ...})","handlingStrategy":"validation","validationCode":"raise ArgumentError, \"course_id requires an Account context\" if group[:course_id].present? && !account.is_a?(Account)","typeGuard":null,"tryCatchPattern":"begin\n  importer.import_group(group)\nrescue Outcomes::Import::InvalidDataError => e\n  retry_with_account_context(group) if group[:course_id].present?\nend","preventionTips":["Pick the importer context (account vs course) based on whether rows carry course_id","Never attach course_id when importing at course level","Document context requirements alongside your import files"],"tags":["validation","permissions","context","outcomes-import"],"backgroundTag":"invalid-argument-value","analyzedSha":"1c9f0bb8013ed69c4f2efe11fd483025469b7e6c","analyzedAt":"2026-09-15T20:33:18.891Z","contentChangedAt":"2026-09-15T20:33:18.891Z","schemaVersion":2},"datasetVersion":"2026-09-23T02:17:17.105Z"}