HumanSignal/label-studio · error · CommandError

Project {project_id} not found

Error message

Project {project_id} not found

What it means

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.

Source

Thrown at label_studio/tasks/management/commands/recalculate_task_counters.py:65

        return (
            Task.objects.filter(project=project)
            .annotate(real_total=Count('annotations', filter=Q(annotations__was_cancelled=False), distinct=True))
            .exclude(total_annotations=F('real_total'))
            .count()
        )

    def handle(self, *args, **options):
        project_id = options.get('project')
        organization_id = options.get('organization')
        dry_run = options.get('dry_run')

        if not project_id and not organization_id:
            raise CommandError('Provide --project <id> or --organization <id>')

        if project_id:
            projects = Project.objects.filter(id=project_id)
            if not projects.exists():
                raise CommandError(f'Project {project_id} not found')
        else:
            projects = Project.objects.filter(organization_id=organization_id)
            if not projects.exists():
                raise CommandError(f'No projects found for organization {organization_id}')

        for project in projects:
            drifted_before = self._drifted_count(project)
            self.stdout.write(
                f'Project {project.id} ({project.title!r}): {drifted_before} task(s) with drifted counters'
            )

            if dry_run:
                continue

            # Recompute counters AND is_labeled (run_sync=True runs the canonical
            # update_tasks_counters + bulk_update_stats_project_tasks inline, batched
            # in transactions), so is_labeled can't be left stale after the counters change.
            updated = project.update_tasks_counters_and_is_labeled(Task.objects.filter(project=project), run_sync=True)

View on GitHub (pinned to 0b49e9b539)

Solutions

  1. List actual project IDs (e.g. via Django shell: Project.objects.values_list('id','title')) and re-run with a valid --project value
  2. Verify the command is connecting to the database/instance you expect (check DATABASE_URL / DJANGO_DB env vars)
  3. If the project was deleted, the ID cannot be recalcuated; use --organization <id> instead to target remaining projects of the org
  4. If you meant to target an org, pass --organization <org_id> instead of --project

Example fix

// before
python manage.py recalculate_task_counters --project 1234   # Project does not exist
// after
python manage.py recalculate_task_counters --project 42     # verified via Project.objects.get(id=42)
Defensive patterns

Strategy: validation

Validate before calling

from tasks.models import Project

def assert_project_exists(project_id):
    if not Project.objects.filter(id=project_id).exists():
        raise SystemExit(f"Project {project_id} does not exist; run with a valid --project or use --organization")

Type guard

def project_exists(project_id) -> bool:
    from tasks.models import Project
    return Project.objects.filter(id=project_id).exists()

Try / catch

from django.core.management import call_command
from django.core.management.base import CommandError
try:
    call_command('recalculate_task_counters', project=str(project_id))
except CommandError as e:
    logger.error("recalculate skipped: %s", e)

Prevention

When it happens

Trigger: 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).

Common situations: 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.

Understand the failure class

Background: 'Could not be found', 'does not exist', 'not found in database': the resource-not-found family when an ID, slug, key, or URI lookup comes back empty — this error's family across 20 libraries.

Related errors


AI-assisted analysis of HumanSignal/label-studio@0b49e9b539 (2026-08-29). Data as JSON: /api/errors/874809adc8193ff7. Report an issue: GitHub.