-
Notifications
You must be signed in to change notification settings - Fork 2.6k
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
Showing config context with multiple tags assigned fails with MultipleObjectsReturned #5387
Comments
I am also seeing this issue. In our case it is related to a device with two tags which provide config context data. Exactly the same netbox version.
|
@lampwins can you take a look at this please? |
It seems like the root issue here is that retrieving the tags assigned to a VM/device requires an from django.db.models import OuterRef, Subquery, Q
from utilities.query_functions import EmptyGroupByJSONBAgg
qs = VirtualMachine.objects.annotate(
config_context_data=Subquery(
ConfigContext.objects.filter(
Q(tags=OuterRef('tags')) | Q(tags=None)
).annotate(
_data=EmptyGroupByJSONBAgg('data', ordering=['weight', 'name'])
).values("_data")
)
).filter(id=5)
>>> print(qs.query)
SELECT "virtualization_virtualmachine"."id", "virtualization_virtualmachine"."created", "virtualization_virtualmachine"."last_updated", "virtualization_virtualmachine"."local_context_data", "virtualization_virtualmachine"."cluster_id", "virtualization_virtualmachine"."tenant_id", "virtualization_virtualmachine"."platform_id", "virtualization_virtualmachine"."name", "virtualization_virtualmachine"."status", "virtualization_virtualmachine"."role_id", "virtualization_virtualmachine"."primary_ip4_id", "virtualization_virtualmachine"."primary_ip6_id", "virtualization_virtualmachine"."vcpus", "virtualization_virtualmachine"."memory", "virtualization_virtualmachine"."disk", "virtualization_virtualmachine"."comments", (SELECT JSONB_AGG(U0."data" ORDER BY U0."weight", U0."name") AS "_data" FROM "extras_configcontext" U0 LEFT OUTER JOIN "extras_configcontext_tags" U1 ON (U0."id" = U1."configcontext_id") WHERE (U1."tag_id" = extras_taggeditem."tag_id" OR U1."tag_id" IS NULL)) AS "config_context_data" FROM "virtualization_virtualmachine" LEFT OUTER JOIN "extras_taggeditem" ON ("virtualization_virtualmachine"."id" = "extras_taggeditem"."object_id" AND ("extras_taggeditem"."content_type_id" = 72)) WHERE "virtualization_virtualmachine"."id" = 5 ORDER BY "virtualization_virtualmachine"."name" ASC, "virtualization_virtualmachine"."id" ASC The SQL query (with some fields remove for brevity) returns the following:
The best approach here isn't immediately clear. However, as this is a critical bug for users who employ config contexts, we'll have to disable the ConfigContextModelQuerySet optimization (introduced in #4559) in the next release if we can't identify a solution. |
Would be happy to see a fix soon as we are heavily using config contexts for our device deployments. |
I finally figured out why this did not come up when this was first raised in #5314. It turns out this happens in two situations and I only found the first one in the fix for #5314. The first instance is when a single config context has two tags assigned and a device/vm has both of those same two tags assigned. That case is actually covered in this test case. The second, outstanding case, is when a device/vm has two tags assigned and those two tags are assigned to two different config context objects. |
…ave multiple tags assigned (#5447)
Environment
Steps to Reproduce
Expected Behavior
See config context
Observed Behavior
See an error
Note: I wrote this already in #5314 (comment) and a change got introduced for 2.9 to fix it but in 2.10 it is still present.
I got asked to create a new issue.
The text was updated successfully, but these errors were encountered: