{"record":{"id":"bc6704199e31934a","repo":"instructure/canvas-lms","slug":"cannot-restore-sub-account-with-id-account-id-because-parent","errorCode":null,"errorMessage":"Cannot restore sub_account with ID: #{account_id} because parent_account with ID: #{parent_account_id} has been deleted.","messagePattern":"Cannot restore sub_account with ID: #(.+?) because parent_account with ID: #(.+?) has been deleted\\.","errorType":"exception","errorClass":"ImportError","httpStatus":null,"severity":"error","filePath":"lib/sis/account_importer.rb","lineNumber":60,"sourceCode":"        @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\n","sourceCodeStart":42,"sourceCodeEnd":78,"githubUrl":"https://github.com/instructure/canvas-lms/blob/1c9f0bb8013ed69c4f2efe11fd483025469b7e6c/lib/sis/account_importer.rb#L42-L78","documentation":"If the referenced parent account exists but its workflow_state is 'deleted', the importer refuses to attach the (new) sub-account to it, raising ImportError. Re-parenting under a deleted account would produce an account buried under an inactive subtree, so it's blocked explicitly.","triggerScenarios":"add_account called with a new account (or one without a valid existing record) whose parent_account_id resolves to an account where parent.workflow_state == 'deleted'.","commonSituations":"Re-importing accounts after the parent was soft-deleted in a previous SIS run; restoring a sub-account while its parent remains deleted; stale SIS data referencing a decommissioned account.","solutions":["Re-activate the parent account first (import it with status 'active'), then import the child.","Re-point the child's parent_account_id to an active account.","Delete the child row if it is no longer needed."],"exampleFix":"// before\nadd_account('sub1', 'old_deleted_parent', 'active', 'Sub', nil)\n// after\nadd_account('old_deleted_parent', nil, 'active', 'Parent', nil)\nadd_account('sub1', 'old_deleted_parent', 'active', 'Sub', nil)","handlingStrategy":"validation","validationCode":"parent = root_account.all_accounts.find_by(sis_source_id: parent_account_id)\nraise \"parent deleted\" if parent&.workflow_state == \"deleted\"","typeGuard":null,"tryCatchPattern":"begin\n  importer.add_account(id, parent_id, status, name, integration_id)\nrescue SIS::BaseImporter::ImportError => e\n  if e.message.include?(\"has been deleted\")\n    reactivate_parent_then_retry\n  end\nend","preventionTips":["Re-activate deleted parents before re-importing their children","Audit workflow_state of referenced parents before import","Tombstone checks: skip rows whose parents were intentionally decommissioned"],"tags":["sis","workflow-state","csv-import"],"backgroundTag":"invalid-state-transition","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"}