{"record":{"id":"1db1263bd4259699","repo":"khoj-ai/khoj","slug":"a-private-agent-with-the-name-instance-name-alre","errorCode":null,"errorMessage":"A private Agent with the name {instance.name} already exists.","messagePattern":"A private Agent with the name (.+?) already exists\\.","errorType":"validation","errorClass":"ValidationError","httpStatus":null,"severity":"error","filePath":"src/khoj/database/models/__init__.py","lineNumber":372,"sourceCode":"        SCHEDULED_JOB = \"scheduled_job\"\n        SCHEDULE_LEADER = \"schedule_leader\"\n        APPLY_MIGRATIONS = \"apply_migrations\"\n\n    # We need to make sure that some operations are thread-safe. To do so, add locks for potentially shared operations.\n    # For example, we need to make sure that only one process is updating the embeddings at a time.\n    name = models.CharField(max_length=200, choices=Operation.choices, unique=True)\n    started_at = models.DateTimeField(auto_now_add=True)\n    max_duration_in_seconds = models.IntegerField(default=60 * 60 * 12)  # 12 hours\n\n\n@receiver(pre_save, sender=Agent)\ndef verify_agent(sender, instance, **kwargs):\n    # check if this is a new instance\n    if instance._state.adding:\n        if Agent.objects.filter(name=instance.name, privacy_level=Agent.PrivacyLevel.PUBLIC).exists():\n            raise ValidationError(f\"A public Agent with the name {instance.name} already exists.\")\n        if Agent.objects.filter(name=instance.name, creator=instance.creator).exists():\n            raise ValidationError(f\"A private Agent with the name {instance.name} already exists.\")\n\n\nclass NotionConfig(DbBaseModel):\n    token = models.CharField(max_length=200)\n    user = models.ForeignKey(KhojUser, on_delete=models.CASCADE)\n\n\nclass GithubConfig(DbBaseModel):\n    pat_token = models.CharField(max_length=200)\n    user = models.ForeignKey(KhojUser, on_delete=models.CASCADE)\n\n\nclass GithubRepoConfig(DbBaseModel):\n    name = models.CharField(max_length=200)\n    owner = models.CharField(max_length=200)\n    branch = models.CharField(max_length=200)\n    github_config = models.ForeignKey(GithubConfig, on_delete=models.CASCADE, related_name=\"githubrepoconfig\")\n","sourceCodeStart":354,"sourceCodeEnd":390,"githubUrl":"https://github.com/khoj-ai/khoj/blob/ae229ca894c0b80ad84664afcfdde523b5e87057/src/khoj/database/models/__init__.py#L354-L390","documentation":"Django pre_save signal rejecting creation of a new Agent whose name collides with an agent created by the same creator. Enforces per-creator name uniqueness for private agents.","triggerScenarios":"A user creates a second agent with a name they already used before (any privacy level), e.g. repeated POST to the agent-creation API with the same name.","commonSituations":"Double-submitted forms; retry of a failed request that actually succeeded; seed scripts re-run for the same user.","solutions":["Rename the new agent to something the user hasn't used.","Return the existing agent instead of creating a duplicate (upsert semantics).","Debounce/disable the create button after first submit to avoid double POSTs."],"exampleFix":"# before\nAgent.objects.create(name=\"MyBot\", creator=user, privacy_level=Agent.PrivacyLevel.PRIVATE)\n\n# after\nagent, created = Agent.objects.get_or_create(\n    name=\"MyBot\", creator=user,\n    defaults={\"privacy_level\": Agent.PrivacyLevel.PRIVATE},\n)","handlingStrategy":"validation","validationCode":"from khoj.database.models import Agent\nagent, created = Agent.objects.get_or_create(\n    name=name, creator=user,\n    defaults={\"privacy_level\": Agent.PrivacyLevel.PRIVATE},\n)","typeGuard":null,"tryCatchPattern":"from django.core.exceptions import ValidationError\ntry:\n    agent.save()\nexcept ValidationError as e:\n    if \"private Agent\" in str(e):\n        agent = Agent.objects.get(name=agent.name, creator=agent.creator)\n    else:\n        raise","preventionTips":["Enforce per-user name uniqueness in the form/serializer with a friendly message.","Use get_or_create or update-or-create semantics for idempotent writes."],"tags":["khoj","django","agent","uniqueness","pre-save-signal"],"backgroundTag":"duplicate-unique-constraint-violation","analyzedSha":"ae229ca894c0b80ad84664afcfdde523b5e87057","analyzedAt":"2026-08-27T03:32:41.843Z","schemaVersion":2},"datasetVersion":"2026-08-27T08:17:20.692Z"}