question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

ManyToMany relation with wrong type

See original GitHub issue

I got into an issue with DjangoObjectType to convert a Django model into a GraphQL type ; On many to many relationships I get a list of the model owning the relation instead of the related model.

Here are the models to reproduce the issue

from django.db import models

class Child(models.Model):
    pass

class Parent(models.Model):
    children = models.ManyToManyField(Child)

and the schema definition

import graphene
from graphene_django import DjangoObjectType
from . import models

class Child(DjangoObjectType):
    class Meta:
        model = models.Child

class Parent(DjangoObjectType):
    class Meta:
        model = models.Parent

class Query(graphene.ObjectType):
    parents = graphene.List(Parent)

    def resolve_parents(self, args, context, info):
        return models.Parent.objects.all()

schema = graphene.Schema(query=Query)

And the test file

import graphene
from django.test import TestCase
from graphene_django.fields import DjangoListField
from . import schema

class M2MTestCase(TestCase):
    def setUp(self):
        schema.schema.build_typemap()
        # get the graphql type for the field related to the children
        self.field_type = schema.Parent._meta.fields['children'].get_type()

    def test_graphql_types(self):
        # we must get a django list field
        self.assertEquals(isinstance(self.field_type, DjangoListField), True)
        # behind it we've a graphql list
        self.assertEquals(isinstance(self.field_type.type, graphene.List), True)
        # it must be a list of Child
        self.assertEquals(self.field_type.type.of_type, schema.Child)

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Reactions:4
  • Comments:8 (1 by maintainers)

github_iconTop GitHub Comments

1reaction
amille44420commented, Aug 30, 2017

So far the only workaround I got is to override the definition myself

class Parent(DjangoObjectType):
    children = graphene.List(Child)

    class Meta:
        model = models.Parent

But I believe we should not have to do that to make it works.

0reactions
stale[bot]commented, Sep 22, 2019

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Wrong column type in jpa many to many relationship
I found my mistake : The wrong type was because of a field type in my database.
Read more >
Many-to-many relationships - Django documentation
Many-to-many relationships¶. To define a many-to-many relationship, use ManyToManyField . ... Adding an object of the wrong type raises TypeError :.
Read more >
Many-to-many relationships in Power BI Desktop
It's now possible to set the relationship cardinality to many-to-many. This setting indicates that neither table contains unique values. For ...
Read more >
Many-to-Many Relationships - Knowing and Avoiding Them
Denormalizing data will require extensive data duplication, row duplication, and result in exponential growth of the database size. For example: ...
Read more >
Hibernate Tips: How to map a bidirectional many-to-many ...
Multiple Authors can write multiple books and a book can be written by one or more authors. That's a typical many-to-many association, and...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found