{"record":{"id":"b8ee65a370c263dd","repo":"makeplane/plane","slug":"failed-to-create-the-instance-admin","errorCode":null,"errorMessage":"Failed to create the instance admin.","messagePattern":"Failed to create the instance admin\\.","errorType":"exception","errorClass":"CommandError","httpStatus":null,"severity":"error","filePath":"apps/api/plane/db/management/commands/create_instance_admin.py","lineNumber":43,"sourceCode":"\n        user = User.objects.filter(email=admin_email).first()\n        if user is None:\n            raise CommandError(\"User with the provided email does not exist.\")\n\n        try:\n            # Get the instance\n            instance = Instance.objects.last()\n\n            # Get or create an instance admin\n            _, created = InstanceAdmin.objects.get_or_create(user=user, instance=instance, role=20)\n\n            if not created:\n                raise CommandError(\"The provided email is already an instance admin.\")\n\n            self.stdout.write(self.style.SUCCESS(\"Successfully created the admin\"))\n        except Exception as e:\n            print(e)\n            raise CommandError(\"Failed to create the instance admin.\")\n","sourceCodeStart":25,"sourceCodeEnd":44,"githubUrl":"https://github.com/makeplane/plane/blob/1c8a60f858d8472aa56e29994ec1c7926da2c6ce/apps/api/plane/db/management/commands/create_instance_admin.py#L25-L44","documentation":"Generic catch-all raised by `create_instance_admin` from the bare `except Exception as e` (lines 41-43). It fires for ANY exception inside the try block — most commonly it MASKS the legitimate 'already an admin' case ([83]) because that CommandError is also an Exception and gets caught here. The original exception is only `print()`-ed to stdout, not surfaced in the CommandError message, making diagnosis hard.","triggerScenarios":"Any failure between lines 30-40: an existing InstanceAdmin (masks [83]); `Instance.objects.last()` returning None combined with a non-null instance constraint; DB integrity error; the `print(e)` line itself.","commonSituations":"Re-running on an already-admin user (most frequent); no `Instance` row exists yet (instance not set up); DB connectivity / constraint violations.","solutions":["Watch stdout for the `print(e)` output immediately before the error — it prints the real cause.","If the user is already an admin, this error is expected; verify with `InstanceAdmin.objects.filter(user=...).exists()`.","Refactor: narrow the except to `IntegrityError`/`DatabaseError` and move the `if not created` branch outside the try so [83] is reported accurately.","Ensure an `Instance` record exists (run instance setup) before promoting admins."],"exampleFix":"# before\nexcept Exception as e:\n    print(e)\n    raise CommandError(\"Failed to create the instance admin.\")\n# after — surface the real cause and stop masking [83]\nexcept (IntegrityError, DatabaseError) as e:\n    raise CommandError(f\"Failed to create the instance admin: {e}\") from e","handlingStrategy":"try-catch","validationCode":"# Pre-flight: instance must exist and user must NOT already be an admin\nfrom plane.license.models import Instance, InstanceAdmin\nassert Instance.objects.exists(), 'No Instance row; run instance setup first'\nassert not InstanceAdmin.objects.filter(user__email=email).exists(), 'already admin'","typeGuard":"def instance_ready_for_admin(email: str) -> bool:\n    from plane.license.models import Instance, InstanceAdmin\n    return Instance.objects.exists() and not InstanceAdmin.objects.filter(user__email=email).exists()","tryCatchPattern":"# The real cause is print()-ed to stdout, not in the CommandError.\n# Capture stdout to recover it, or pre-validate as above.\nfrom io import StringIO\nout = StringIO()\ntry:\n    call_command('create_instance_admin', email, stdout=out)\nexcept CommandError:\n    print('real cause was printed to stdout:', out.getvalue())","preventionTips":["Watch stdout (the real exception is print()-ed, not in the CommandError).","Ensure an Instance row exists before promoting admins.","Pre-check whether the user is already an admin to avoid the masked [83]->[84] path.","Refactor the command to narrow the except clause."],"tags":["django","management-command","exception-handling","exception-masking","plane","bug"],"backgroundTag":null,"analyzedSha":"1c8a60f858d8472aa56e29994ec1c7926da2c6ce","analyzedAt":"2026-08-12T14:44:31.636Z","schemaVersion":2},"datasetVersion":"2026-08-12T18:17:37.767Z"}