blb-ventures / strawberry-django-plus

Enhanced Strawberry GraphQL integration with Django
MIT License
179 stars 47 forks source link

Querying both sides of a OneToOneField at the same time is an error #222

Closed coderanger closed 1 year ago

coderanger commented 1 year ago

DB Model:

    cooking_recipe_item = models.OneToOneField(
        "Item",
        on_delete=models.SET_NULL,
        related_name="cooking_recipe_cookable",
        null=True,
        blank=True,
    )

GraphQL Model:

    cooking_recipe_item: Optional["Item"]
    cooking_recipe_cookable: Optional["Item"]

Query:

query {
  item {
    cookingRecipeItem {
      name
    }
    cookingRecipeCookable {
      name
    }
  }
}

Error:

Field Item.cooking_recipe_item cannot be both deferred and traversed using select_related at the same time.

It works fine if only one direction or the other is in the query. And it works with ForeignKeys in the same kind of configuration.

bellini666 commented 1 year ago

Hey @coderanger ,

Hrm, I'm trying to understand your modeling there. Is it a OneToOne to the same item (self)? Because your are querying both the foreignkey and the related item in the same object.

bellini666 commented 1 year ago

Btw, which django version are you using?

Django 4.2.2 just got released today, and one of the fixes is this: https://code.djangoproject.com/ticket/34612

I'm wondering if it has anything to do with the issue

coderanger commented 1 year ago

Yes, sorry it's a self-referencing field https://github.com/coderanger/farmrpg-etl2/blob/06920220df55376277462749b82bf046994a0dca/items/models.py#L39-L45 . I switched it to a FK since that works and it's easy enough to add a ...[0] in the client code but it used to be a OneToOne instead.

It's 4.2 right now, will give the new release a try and report back.

bellini666 commented 1 year ago

@coderanger please tell me if 4.2.2 fixed the issue for you, so we can close this issue. Otherwise I'll need to investigate what might be going on here =P

coderanger commented 1 year ago

Apologies, got distracted. I tried it just now and it appears to work with 4.2.2 so I think we can close this out as a Django bug. If I find a new way to trigger it, I'll reopen :)