{"record":{"id":"874809adc8193ff7","repo":"HumanSignal/label-studio","slug":"project-project-id-not-found","errorCode":null,"errorMessage":"Project {project_id} not found","messagePattern":"Project (.+?) not found","errorType":"console","errorClass":"CommandError","httpStatus":null,"severity":"error","filePath":"label_studio/tasks/management/commands/recalculate_task_counters.py","lineNumber":65,"sourceCode":"        return (\n            Task.objects.filter(project=project)\n            .annotate(real_total=Count('annotations', filter=Q(annotations__was_cancelled=False), distinct=True))\n            .exclude(total_annotations=F('real_total'))\n            .count()\n        )\n\n    def handle(self, *args, **options):\n        project_id = options.get('project')\n        organization_id = options.get('organization')\n        dry_run = options.get('dry_run')\n\n        if not project_id and not organization_id:\n            raise CommandError('Provide --project <id> or --organization <id>')\n\n        if project_id:\n            projects = Project.objects.filter(id=project_id)\n            if not projects.exists():\n                raise CommandError(f'Project {project_id} not found')\n        else:\n            projects = Project.objects.filter(organization_id=organization_id)\n            if not projects.exists():\n                raise CommandError(f'No projects found for organization {organization_id}')\n\n        for project in projects:\n            drifted_before = self._drifted_count(project)\n            self.stdout.write(\n                f'Project {project.id} ({project.title!r}): {drifted_before} task(s) with drifted counters'\n            )\n\n            if dry_run:\n                continue\n\n            # Recompute counters AND is_labeled (run_sync=True runs the canonical\n            # update_tasks_counters + bulk_update_stats_project_tasks inline, batched\n            # in transactions), so is_labeled can't be left stale after the counters change.\n            updated = project.update_tasks_counters_and_is_labeled(Task.objects.filter(project=project), run_sync=True)","sourceCodeStart":47,"sourceCodeEnd":83,"githubUrl":"https://github.com/HumanSignal/label-studio/blob/0b49e9b53917880baf1dd85d574fe5541a9aafb2/label_studio/tasks/management/commands/recalculate_task_counters.py#L47-L83","documentation":"The Django management command `recalculate_task_counters` was invoked with a --project <id> argument, but no Project with that primary key exists in the database. The command raises CommandError immediately, before any counters are recalculated, to avoid silently doing nothing.","triggerScenarios":"Running `python label_studio/manage.py recalculate_task_counters --project <id>` where <id> does not match any Project.id (typo, deleted project, wrong environment/database, or stale ID copied from another instance).","commonSituations":"Copy-pasting a project ID from a local dev instance while running the command against production; running the command after the project was deleted; connecting to the wrong DB due to misconfigured DJANGO_DB settings; using the organization numeric ID by mistake with --project.","solutions":["List actual project IDs (e.g. via Django shell: Project.objects.values_list('id','title')) and re-run with a valid --project value","Verify the command is connecting to the database/instance you expect (check DATABASE_URL / DJANGO_DB env vars)","If the project was deleted, the ID cannot be recalcuated; use --organization <id> instead to target remaining projects of the org","If you meant to target an org, pass --organization <org_id> instead of --project"],"exampleFix":"// before\npython manage.py recalculate_task_counters --project 1234   # Project does not exist\n// after\npython manage.py recalculate_task_counters --project 42     # verified via Project.objects.get(id=42)","handlingStrategy":"validation","validationCode":"from tasks.models import Project\n\ndef assert_project_exists(project_id):\n    if not Project.objects.filter(id=project_id).exists():\n        raise SystemExit(f\"Project {project_id} does not exist; run with a valid --project or use --organization\")","typeGuard":"def project_exists(project_id) -> bool:\n    from tasks.models import Project\n    return Project.objects.filter(id=project_id).exists()","tryCatchPattern":"from django.core.management import call_command\nfrom django.core.management.base import CommandError\ntry:\n    call_command('recalculate_task_counters', project=str(project_id))\nexcept CommandError as e:\n    logger.error(\"recalculate skipped: %s\", e)","preventionTips":["Fetch project IDs from the target instance (Project.objects.values_list('id')) before running the command","Confirm DATABASE_URL/env points at the instance you intend","Never hardcode project IDs across environments; script the lookup","Prefer --organization when operating across many projects of an org"],"tags":["django","management-command","cli","not-found"],"backgroundTag":"resource-not-found","analyzedSha":"0b49e9b53917880baf1dd85d574fe5541a9aafb2","analyzedAt":"2026-08-29T00:39:52.578Z","schemaVersion":2},"datasetVersion":"2026-08-29T02:17:18.158Z"}