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

Nested fields filters on self referenced entities causing an SQLAlchemy error #11

Closed
alonroth opened this issue Jan 10, 2020 · 1 comment
Assignees
Labels
bug Something isn't working

Comments

@alonroth
Copy link

First of all thanks for the awesome library! :)

I have a system with this sort of hierarchy:

  • Chapter
    -- Chapter
    --- Checklist
    --- Checklist
  • Chapter
    -- Checklist

Chapter.py

class Chapter(db.Model):
    id = db.Column(db.Integer, primary_key=True, autoincrement=True)
    name = db.Column(db.String, nullable=False)
    chapter_id = db.Column(ForeignKey('go_tab_chapters.id'))
    sub_chapters = relationship('Chapter', backref=backref('chapter', remote_side=[id]),
                                primaryjoin=id == chapter_id)
    checklists = relationship(Checklist, backref=backref('chapter'), uselist=True)

Checklist.py

class Checklist(db.Model):
    id = db.Column(db.Integer, primary_key=True, autoincrement=True)
    name = db.Column(db.String, nullable=False)
    chapter_id = db.Column(ForeignKey('go_tab_chapters.id'))

This is my GraphQL entities:

class ChapterFilter(FilterSet):
    class Meta:
        model = Chapter
        fields = {'name': [...]}

class ChecklistFilter(FilterSet):
    class Meta:
        model = Checklist
        fields = {
            'name': [...]
        }

class CustomField(FilterableConnectionField):
    filters = {
        Chapter: ChapterFilter(),
        Checklist: ChecklistFilter()
    }

class ChapterNode(SQLAlchemyObjectType):
    class Meta:
        model = Chapter
        interfaces = (graphene.relay.Node,)
        connection_field_factory = CustomField.factory

class ChapterConnection(Connection):
    class Meta:
        node = ChapterNode

class ChecklistNode(SQLAlchemyObjectType):
    class Meta:
        model = Checklist
        interfaces = (graphene.relay.Node,)
        connection_field_factory = CustomField.factory

class ChecklistConnection(Connection):
    class Meta:
        node = ChecklistNode

When I'm trying to run the following query:

{
  chapters (filters:{
    name: "abc"
  }){
    edges{
      node{
        name
        checklists (filters:{
          name: "abc"
        }){
          edges{
            node{
              name
            }
          }
        }
        subChapters{
          edges{
            node{
              name
            }
          }
        }
      }
    }
  }
}

I get the following error:
graphql.error.located_error.GraphQLLocatedError: Can't construct a join from Mapper|Chapter|chapters to Mapper|Chapter|chapters, they are the same entity

When I comment the following line the query works but not the nested filter:

class ChapterNode(SQLAlchemyObjectType):
    class Meta:
        model = Chapter
        interfaces = (graphene.relay.Node,)
        # connection_field_factory = CustomField.factory

@art1415926535 , Can you please help?

Many thanks!

@art1415926535 art1415926535 self-assigned this Jan 13, 2020
@art1415926535 art1415926535 added the bug Something isn't working label Jan 13, 2020
@art1415926535
Copy link
Owner

art1415926535 commented Jan 17, 2020

Fixed in version 1.10.2.

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