instructure/canvas-lms · error · ImportError

Only one context is allowed and both course_id and…

Error message

Only one context is allowed and both course_id and account_id where provided for #{type_display} #{sis_id}.

What it means

find_context resolves exactly one owning context (Account or Course) for an imported group/category/tag. Supplying both course_id and account_id is ambiguous — the importer cannot decide where the record belongs — so it raises ImportError immediately.

Solutions

  1. Populate only one of account_id / course_id for each row — leave the other blank.
  2. If the category is course-scoped, clear the account_id column; if account-scoped, clear course_id.
  3. Fix the exporter so account-level records omit course_id and vice versa.

Example fix

// before (both columns set)
gc-1,Math Groups,ACCOUNT-1,COURSE-7,active
// after (course-scoped)
gc-1,Math Groups,,COURSE-7,active
Defensive patterns

Strategy: validation

Validate before calling

if account_id.present? && course_id.present?
  raise ArgumentError, 'provide either account_id or course_id, not both'
end

Try / catch

begin
  importer.add_group(sis_id, acct_id, course_id, name, status)
rescue SIS::GroupImporter::ImportError => e
  notify("ambiguous context for #{sis_id}: #{e.message}")
end

Prevention

When it happens

Trigger: add_group_category(sis_id, account_id, course_id, name, status), add_group(...), add_tag(...) or add_differentiation_tag_set(...) called with both a non-nil account_id and a non-nil course_id (both columns populated in the CSV row).

Common situations: CSV export tools that always emit both account_id and course_id columns and fill both; manually adding a course_id to an account-level category row without clearing account_id.

Related errors


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

Appendix: source

Thrown at lib/sis/group_category_importer.rb:57

        @roll_back_data = []
        @accounts_cache = {}
        @courses_cache = {}
      end

      def invalid_category?(sis_id, name, status, type_display)
        raise ImportError, "No sis_id given for a #{type_display}" if sis_id.blank?
        raise ImportError, "No name given for #{type_display} #{sis_id}" if name.blank?
        raise ImportError, "No status given for #{type_display} #{sis_id}" if status.blank?
        raise ImportError, "Improper status \"#{status}\" for #{type_display} #{sis_id}, skipping" unless /\A(active|deleted)/i.match?(status)
        return true if @batch.skip_deletes? && status =~ /deleted/i

        false
      end

      def find_context(account_id, course_id, sis_id, type_display)
        context = nil
        if account_id && course_id
          raise ImportError, "Only one context is allowed and both course_id and account_id where provided for #{type_display} #{sis_id}."
        end

        if account_id
          context = @accounts_cache[account_id]
          context ||= @root_account.all_accounts.active.find_by(sis_source_id: account_id)
          raise ImportError, "Account with id \"#{account_id}\" didn't exist for #{type_display} #{sis_id}" unless context

          @accounts_cache[context.sis_source_id] = context
        end

        if course_id
          context = @courses_cache[course_id]
          context ||= @root_account.all_courses.active.find_by(sis_source_id: course_id)
          raise ImportError, "Course with id \"#{course_id}\" didn't exist for #{type_display} #{sis_id}" unless context

          @courses_cache[context.sis_source_id] = context
        elsif course_id.nil? && type_display == "differentiation tag set"
          raise ImportError, "No course_id given for #{type_display} #{sis_id}"

View on GitHub (pinned to 1c9f0bb801)