{"id":9792,"library":"graphene-sqlalchemy-filter","title":"Graphene SQLAlchemy Filter","description":"graphene-sqlalchemy-filter provides a flexible way to generate GraphQL filter arguments for SQLAlchemy models, enabling complex queries and sorting directly through your GraphQL API. As of version 1.14.0, it offers robust filtering capabilities compatible with SQLAlchemy 1.4+ and Graphene SQLAlchemy integrations. The library maintains an active development pace, regularly releasing updates with bug fixes and feature enhancements.","status":"active","version":"1.14.0","language":"en","source_language":"en","source_url":"https://github.com/art1415926535/graphene-sqlalchemy-filter","tags":["graphene","sqlalchemy","graphql","filter","query","api"],"install":[{"cmd":"pip install graphene-sqlalchemy-filter","lang":"bash","label":"Install stable version"}],"dependencies":[{"reason":"Core integration with Graphene and SQLAlchemy ORM.","package":"graphene-sqlalchemy","optional":false},{"reason":"The underlying ORM for database interaction.","package":"SQLAlchemy","optional":false},{"reason":"GraphQL framework.","package":"graphene","optional":false}],"imports":[{"symbol":"FilterSet","correct":"from graphene_sqlalchemy_filter import FilterSet"},{"note":"Use FilterableConnectionField for filter capabilities, not the base SQLAlchemyConnectionField.","wrong":"from graphene_sqlalchemy import SQLAlchemyConnectionField","symbol":"FilterableConnectionField","correct":"from graphene_sqlalchemy_filter import FilterableConnectionField"},{"symbol":"ModelLoader","correct":"from graphene_sqlalchemy_filter import ModelLoader"}],"quickstart":{"code":"import graphene\nimport sqlalchemy as sa\nfrom sqlalchemy.orm import declarative_base, sessionmaker, relationship\nfrom graphene_sqlalchemy import SQLAlchemyObjectType\nfrom graphene_sqlalchemy_filter import FilterableConnectionField, FilterSet\n\n# 1. Setup SQLAlchemy (using in-memory SQLite for example)\nBase = declarative_base()\n\nclass UserModel(Base):\n    __tablename__ = 'users'\n    id = sa.Column(sa.Integer, primary_key=True)\n    name = sa.Column(sa.String, nullable=False)\n    email = sa.Column(sa.String, unique=True, nullable=False)\n    posts = relationship(\"PostModel\", back_populates=\"author\")\n\nclass PostModel(Base):\n    __tablename__ = 'posts'\n    id = sa.Column(sa.Integer, primary_key=True)\n    title = sa.Column(sa.String, nullable=False)\n    content = sa.Column(sa.String)\n    author_id = sa.Column(sa.Integer, sa.ForeignKey('users.id'))\n    author = relationship(\"UserModel\", back_populates=\"posts\")\n\nengine = sa.create_engine('sqlite:///:memory:')\nBase.metadata.create_all(engine)\nSession = sessionmaker(bind=engine)\nsession = Session()\n\n# Add some initial data\nuser1 = UserModel(name=\"Alice\", email=\"alice@example.com\")\nuser2 = UserModel(name=\"Bob\", email=\"bob@example.com\")\nsession.add_all([user1, user2])\nsession.commit()\nsession.refresh(user1)\nsession.refresh(user2)\n\npost1 = PostModel(title=\"First Post\", content=\"Hello world\", author=user1)\npost2 = PostModel(title=\"Second Post\", content=\"Another one\", author=user2)\nsession.add_all([post1, post2])\nsession.commit()\n\n# 2. Define Graphene ObjectTypes\nclass Post(SQLAlchemyObjectType):\n    class Meta:\n        model = PostModel\n        interfaces = (graphene.relay.Node,)\n\nclass User(SQLAlchemyObjectType):\n    class Meta:\n        model = UserModel\n        interfaces = (graphene.relay.Node,)\n\n# 3. Define FilterSet for your models\nclass UserFilter(FilterSet):\n    class Meta:\n        model = UserModel\n        fields = {\n            'name': ['eq', 'like'],\n            'email': ['eq'],\n            'id': ['eq', 'in']\n        }\n\nclass PostFilter(FilterSet):\n    class Meta:\n        model = PostModel\n        fields = {\n            'title': ['eq', 'like'],\n            'content': ['like'],\n            'author_id': ['eq'],\n        }\n\n# 4. Define your Graphene Query with FilterableConnectionField\nclass Query(graphene.ObjectType):\n    node = graphene.relay.Node.Field()\n\n    all_users = FilterableConnectionField(User.connection, filters=UserFilter())\n    all_posts = FilterableConnectionField(Post.connection, filters=PostFilter())\n\n    def resolve_all_users(root, info, **kwargs):\n        return User.get_query(info).session(session)\n\n    def resolve_all_posts(root, info, **kwargs):\n        return Post.get_query(info).session(session)\n\nschema = graphene.Schema(query=Query)\n\n# Example GraphQL query (for demonstration, not part of quickstart output)\n# query = \"\"\"\n#     query {\n#       allUsers(filters: {name_like: \"Ali\"}) {\n#         edges {\n#           node {\n#             id\n#             name\n#             email\n#             posts {\n#                 edges {\n#                     node {\n#                         title\n#                     }\n#                 }\n#             }\n#           }\n#         }\n#       }\n#       allPosts(filters: {title_eq: \"First Post\"}) {\n#         edges {\n#           node {\n#             title\n#             author {\n#               name\n#             }\n#           }\n#         }\n#       }\n#     }\n# \"\"\"\n# result = schema.execute(query)\n# if result.errors: print(result.errors)\n# else: print(result.data)","lang":"python","description":"This quickstart demonstrates how to set up a basic GraphQL schema using `graphene-sqlalchemy-filter` with an in-memory SQLite database. It defines SQLAlchemy models, Graphene object types, and `FilterSet` classes. The `FilterableConnectionField` is then used in the root query to enable filtering and pagination on collections of objects. The example populates some initial data and sets up a runnable schema."},"warnings":[{"fix":"Upgrade to `graphene-sqlalchemy-filter>=1.13.0`.","message":"Version 1.13.0 introduced changes for compatibility with SQLAlchemy 1.4+. If you are upgrading SQLAlchemy to 1.4+ and encounter issues, ensure `graphene-sqlalchemy-filter` is also updated.","severity":"breaking","affected_versions":"<1.13.0"},{"fix":"Upgrade to `graphene-sqlalchemy-filter>=1.14.0` to resolve SQLAlchemy 2.0 deprecation warnings.","message":"Versions prior to 1.14.0 might produce `RemovedIn20Warning` when used with newer SQLAlchemy versions, indicating potential future incompatibility with SQLAlchemy 2.0.","severity":"gotcha","affected_versions":"<1.14.0"},{"fix":"Upgrade to `graphene-sqlalchemy-filter>=1.12.2` to ensure proper handling of enums and hybrid properties.","message":"Incorrect filtering or errors might occur with `Enum` types or `hybrid_property` attributes on versions older than 1.12.x.","severity":"gotcha","affected_versions":"<1.12.2"},{"fix":"Upgrade to `graphene-sqlalchemy-filter>=1.14.0` which includes fixes for several join filters with the same table.","message":"When constructing complex queries involving multiple join filters on the same table, older versions might yield incorrect results or unexpected behavior.","severity":"gotcha","affected_versions":"<1.14.0"}],"env_vars":null,"last_verified":"2026-04-17T00:00:00.000Z","next_check":"2026-07-16T00:00:00.000Z","problems":[{"fix":"Ensure you are on `graphene-sqlalchemy-filter>=1.12.2`. Additionally, verify your SQLAlchemy enum definitions or hybrid property implementations handle potential `None` values gracefully.","cause":"Often related to issues processing enum types or hybrid properties in versions prior to 1.12.x, especially when the underlying SQLAlchemy model data could be `None` or malformed in a specific context.","error":"TypeError: 'NoneType' object is not subscriptable"},{"fix":"Replace `graphene_sqlalchemy.SQLAlchemyConnectionField` with `graphene_sqlalchemy_filter.FilterableConnectionField` in your Graphene schema definitions to enable filtering capabilities.","cause":"This error typically occurs when attempting to use filter or sorting logic directly with `graphene_sqlalchemy.SQLAlchemyConnectionField`, which lacks the necessary filter integration.","error":"AttributeError: 'SQLAlchemyConnectionField' object has no attribute 'aliased'"},{"fix":"Review your `FilterSet.Meta.fields` configuration. For each field, ensure the list of allowed operators includes the one you are trying to use in your GraphQL query. For example: `'my_field': ['eq', 'like']`.","cause":"This means you've attempted to use a filter operator (e.g., `_gt`, `_like`, `_in`) that was not explicitly defined or allowed for `some_field` in your `FilterSet.Meta.fields` configuration.","error":"ValueError: Unknown filter argument 'some_field_unknown_filter'"},{"fix":"For ambiguous relationships, explicitly define `primaryjoin` and `foreign_keys` arguments in your SQLAlchemy `relationship` definitions to guide the ORM in constructing joins. Simplify your GraphQL filter query if possible.","cause":"This can happen in complex queries with multiple relationships, especially when filters are applied to related models, and SQLAlchemy struggles to infer the correct join conditions.","error":"sqlalchemy.exc.CompileError: (in _create_join_clause) Can't find column..."}]}