{"record":{"id":"b5bda467249c751c","repo":"instructure/canvas-lms","slug":"parent-account-didn-t-exist-for-account-id","errorCode":null,"errorMessage":"Parent account didn't exist for #{account_id}","messagePattern":"Parent account didn't exist for #(.+?)","errorType":"exception","errorClass":"ImportError","httpStatus":null,"severity":"error","filePath":"lib/sis/account_importer.rb","lineNumber":59,"sourceCode":"      def initialize(batch, root_account, logger)\n        @batch = batch\n        @root_account = root_account\n        @accounts_cache = {}\n        @roll_back_data = []\n        @logger = logger\n        @success_count = 0\n        @accounts_to_set_sis_batch_ids = Set.new\n      end\n\n      def add_account(account_id, parent_account_id, status, name, integration_id)\n        raise ImportError, \"No account_id given for an account\" if account_id.blank?\n        return if @batch.skip_deletes? && status =~ /deleted/i\n\n        parent = nil\n        unless parent_account_id.blank?\n          parent = @accounts_cache[parent_account_id]\n          parent ||= @root_account.all_accounts.find_by(sis_source_id: parent_account_id)\n          raise ImportError, \"Parent account didn't exist for #{account_id}\" unless parent\n          raise ImportError, \"Cannot restore sub_account with ID: #{account_id} because parent_account with ID: #{parent_account_id} has been deleted.\" if parent.workflow_state == \"deleted\"\n\n          @accounts_cache[parent.sis_source_id] = parent\n        end\n\n        account = @accounts_cache[account_id]\n        account ||= @root_account.all_accounts.find_by(sis_source_id: account_id)\n        if account.nil?\n          raise ImportError, \"No name given for account #{account_id}, skipping\" if name.blank?\n          raise ImportError, \"Improper status \\\"#{status}\\\" for account #{account_id}, skipping\" unless /\\A(active|deleted)/i.match?(status)\n        end\n\n        account ||= @root_account.sub_accounts.new\n\n        account.root_account = @root_account\n        if account.new_record? || !account.stuck_sis_fields.include?(:parent_account_id) || Account.sis_stickiness_options[:add_sis_stickiness]\n          account.parent_account = parent || @root_account\n        end","sourceCodeStart":41,"sourceCodeEnd":77,"githubUrl":"https://github.com/instructure/canvas-lms/blob/1c9f0bb8013ed69c4f2efe11fd483025469b7e6c/lib/sis/account_importer.rb#L41-L77","documentation":"When an account row specifies a parent_account_id, the importer resolves it from the accounts cache or via root_account.all_accounts.find_by(sis_source_id: ...). If no account with that SIS id exists, the parent reference cannot be satisfied and ImportError is raised. This prevents creating orphaned sub-accounts pointing at nonexistent parents.","triggerScenarios":"add_account called with a parent_account_id that was never imported (or imported after this row), was deleted with its SIS id removed, or is misspelled/differs in case from the parent's sis_source_id.","commonSituations":"Row ordering in accounts.csv where child appears before parent; typos in parent_account_id; parent account deleted in a prior SIS run; importing a subset of accounts so the parent is absent from the batch and DB.","solutions":["Ensure the parent account row exists earlier in accounts.csv (or exists in Canvas with that sis_source_id).","Fix typos/case mismatches in the parent_account_id value.","Create/import the parent account before the child in the same SIS batch."],"exampleFix":"// before\nadd_account('sub1', 'nonexistent_parent', 'active', 'Sub', nil)\n// after\nadd_account('parent1', nil, 'active', 'Parent', nil)\nadd_account('sub1', 'parent1', 'active', 'Sub', nil)","handlingStrategy":"validation","validationCode":"unless parent_account_id.blank?\n  found = root_account.all_accounts.exists?(sis_source_id: parent_account_id)\n  raise \"unknown parent #{parent_account_id}\" unless found || batch.includes_account?(parent_account_id)\nend","typeGuard":null,"tryCatchPattern":"begin\n  importer.add_account(id, parent_id, status, name, integration_id)\nrescue SIS::BaseImporter::ImportError => e\n  logger.error(\"#{e.message}; ensure parents are ordered before children in accounts.csv\")\nend","preventionTips":["Order accounts.csv parents-before-children","Run a referential-integrity pass over the CSV before import","Keep sis_source_ids stable across SIS runs"],"tags":["sis","foreign-key","csv-import"],"backgroundTag":"entity-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"}