Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MultipleObjectsReturned Error in Signal If Job Class Name is not Unique #766

Open
bryanculver opened this issue May 29, 2024 · 1 comment
Labels
bug Something isn't working

Comments

@bryanculver
Copy link
Member

Offending line of code here:

deploy_job_button = Job.objects.get(job_class_name="DeployConfigPlanJobButtonReceiver")

Should a user duplicate the class name of a Job in their code (for example cribbing it and not knowing the class name should be unique) can result in Nautobot failing to run post migrate, with the resulting output:

│   File "/usr/local/lib/python3.11/site-packages/nautobot/core/apps/__init__.py", line 795, in post_migrate_send_nautobot_database_ready                                                                         │
│     nautobot_database_ready.send(sender=app_conf, app_config=app_conf, **kwargs)                                                                                                                                │
│   File "/usr/local/lib/python3.11/site-packages/django/dispatch/dispatcher.py", line 180, in send                                                                                                               │
│     return [                                                                                                                                                                                                    │
│            ^                                                                                                                                                                                                    │
│   File "/usr/local/lib/python3.11/site-packages/django/dispatch/dispatcher.py", line 181, in <listcomp>                                                                                                         │
│     (receiver, receiver(signal=self, sender=sender, **named))                                                                                                                                                   │
│                ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^                                                                                                                                                    │
│   File "/usr/local/lib/python3.11/site-packages/nautobot_golden_config/signals.py", line 60, in post_migrate_create_job_button                                                                                  │
│     deploy_job_button = Job.objects.get(job_class_name="DeployConfigPlanJobButtonReceiver")                                                                                                                     │
│                         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^                                                                                                                     │
│   File "/usr/local/lib/python3.11/site-packages/django/db/models/manager.py", line 85, in manager_method                                                                                                        │
│     return getattr(self.get_queryset(), name)(*args, **kwargs)                                                                                                                                                  │
│            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^                                                                                                                                                  │
│   File "/usr/local/lib/python3.11/site-packages/django/db/models/query.py", line 439, in get                                                                                                                    │
│     raise self.model.MultipleObjectsReturned(                                                                                                                                                                   │
│ __fake__.Job.MultipleObjectsReturned: get() returned more than one Job -- it returned 2!                                                                                                                        │
│ ❌ Waited 30s or more for the DB to become ready.

The Job lookup should include the full path:

>>> Job.objects.get(job_class_name="DeployConfigPlanJobButtonReceiver", module_name="nautobot_golden_config.jobs")
<Job: Deploy Config Plan (Job Button Receiver)>
@bryanculver
Copy link
Member Author

Another option:

Job.objects.get_for_class_path(class_path="nautobot_golden_config.jobs.DeployConfigPlanJobButtonReceiver")

@gsnider2195 gsnider2195 added the bug Something isn't working label Oct 25, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants