{"record":{"id":"4d3f3f15b6f62b5a","repo":"instructure/canvas-lms","slug":"account-not-found-account-id-for-course-course-id","errorCode":null,"errorMessage":"Account not found \"#{account_id}\" for course #{course_id}","messagePattern":"Account not found \"#(.+?)\" for course #(.+?)","errorType":"exception","errorClass":"ImportError","httpStatus":null,"severity":"error","filePath":"lib/sis/course_importer.rb","lineNumber":96,"sourceCode":"          if course.nil?\n            course = Course.new\n            state_changes << :created\n          else\n            state_changes << :updated\n          end\n          course.saved_by = :sis_import\n          course_enrollment_term_id_stuck = course.stuck_sis_fields.include?(:enrollment_term_id)\n          if !course_enrollment_term_id_stuck && term_id\n            term = @root_account.enrollment_terms.active.find_by(sis_source_id: term_id)\n          end\n          course.enrollment_term = term if term\n          course.root_account = @root_account\n\n          account = nil\n          account = @root_account.all_accounts.find_by(sis_source_id: account_id) if account_id.present?\n          account ||= @root_account.all_accounts.find_by(sis_source_id: fallback_account_id) if fallback_account_id.present?\n          if account_id.present? && !account\n            raise ImportError, \"Account not found \\\"#{account_id}\\\" for course #{course_id}\"\n          end\n\n          course_account_stuck = course.stuck_sis_fields.include?(:account_id)\n          if !course_account_stuck && account&.active?\n            course.account = account\n          end\n\n          if course.account&.deleted?\n            raise ImportError, \"Cannot restore course #{course_id} because the associated account #{course.account.sis_source_id} is deleted\"\n          end\n\n          course.account ||= @root_account\n\n          update_account_associations = course.account_id_changed? || course.root_account_id_changed?\n\n          if account&.enable_as_k5_account? && friendly_name.present?\n            course.friendly_name = friendly_name\n          end","sourceCodeStart":78,"sourceCodeEnd":114,"githubUrl":"https://github.com/instructure/canvas-lms/blob/1c9f0bb8013ed69c4f2efe11fd483025469b7e6c/lib/sis/course_importer.rb#L78-L114","documentation":"SisCourseImporter#add_course raises ImportError when account_id is present but no account in the root account's account tree has that sis_source_id. The importer looks up accounts by SIS id via @root_account.all_accounts; a non-blank account_id that resolves to nothing is a hard error, whereas a blank account_id silently falls back to fallback_account_id or the root account.","triggerScenarios":"add_course called with account_id=\"acc-42\" where no account with sis_source_id 'acc-42' exists under the root account — e.g. the account was deleted, lives under a different root, the sis_source_id was renamed, or the accounts.csv containing it was not imported (or imported in a later batch).","commonSituations":"courses.csv imported before its companion accounts.csv (ordering issue); account deleted in the SIS of record but still referenced by course rows; cross-subaccount typos or renames; multi-tenant setups where the account belongs to another root account.","solutions":["Ensure accounts.csv is imported before courses.csv in the same batch so referenced accounts exist","Verify the account's sis_source_id matches exactly (case, whitespace, no renames) in accounts.csv","Confirm the account belongs to the same root account the importer is running against","If the account was intentionally removed, update or remove the course rows referencing it","Use fallback_account_id (or leave account_id blank) if the course should land in a default subaccount"],"exampleFix":"// before\ncourse.add_course(id, term, \"dept-eng-42\", nil, status, ...) # dept-eng-42 not yet imported\n// after: import accounts first, or fall back\nimporter.add_course(id, term, \"dept-eng-42\", \"root-dept\", status, ...) # fallback if primary missing","handlingStrategy":"validation","validationCode":"known_accounts = root_account.all_accounts.pluck(:sis_source_id)\nif row['account_id'].present? && !known_accounts.include?(row['account_id'])\n  raise ArgumentError, \"unknown account_id #{row['account_id']} for course #{row['course_id']}\"\nend","typeGuard":"nil","tryCatchPattern":"begin\n  importer.add_course(...)\nrescue SisImport::ImportError => e\n  raise unless e.message.start_with?('Account not found')\n  failures << e.message # collect and re-run after accounts.csv is fixed/imported\nend","preventionTips":["Always import accounts.csv before courses.csv","Keep account sis_source_ids immutable once assigned; never rename in place","Diff course rows' account_ids against existing accounts each batch","Provide fallback_account_id for resilience to missing subaccounts"],"tags":["sis-import","account-lookup","referential-integrity"],"backgroundTag":"record-not-found","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"}