graphene-sqlalchemy: AssertionError: Found different types with the same name in the schema

I have two Classes Products and SalableProducts in my Models (SalableProducts inherits from Products so it has every field of it’s database), in my Schema here is what i did

class  Product(SQLAlchemyObjectType):
    class Meta:
        model = ProductModel
        interfaces = (relay.Node, )
        
class ProductConnections(relay.Connection):
    class Meta:
        node = Product
class  SalableProduct(SQLAlchemyObjectType):
    class Meta:
        model = SalableProductModel
        interfaces = (relay.Node, )

class  SalableProductConnections(relay.Connection):
    class Meta:
        node = SalableProduct

and here is my Query class :

class Query(graphene.ObjectType):
    node = relay.Node.Field()
    all_products = SQLAlchemyConnectionField(ProductConnections)
    all_salable_products = SQLAlchemyConnectionField(SalableProductConnections)  

When i run my server i got this error :

AssertionError: Found different types with the same name in the schema: product_status, product_status.

About this issue

  • Original URL
  • State: open
  • Created 8 years ago
  • Reactions: 4
  • Comments: 19 (4 by maintainers)

Most upvoted comments

I use this monkey-patch on my projects:

from functools import lru_cache

graphene.Enum.from_enum = lru_cache(maxsize=None)(graphene.Enum.from_enum)

So, for the record, I solved this by making global SQLAlchemy types, so @richin13 , your code would become:

SA_AdStatus = db.Enum(AdStatus)

class MyModel(db.Model):
    status_before = db.Column(SA_AdStatus, nullable=False)
    status_during = db.Column(SA_AdStatus, nullable=False)

I’m running into this issue. I defined my model like this:

class MyModel(db.Model):
    status_before = db.Column(db.Enum(AdStatus), nullable=False)
    status_during = db.Column(db.Enum(AdStatus), nullable=False)

Which fails with:

AssertionError

AssertionError: Found different types with the same name in the schema: AdStatus, AdStatus.

Versions:

flask-graphql       2.0.0   Adds GraphQL support to your Flask application
graphene            2.1.8   GraphQL Framework for Python
graphene-sqlalchemy 2.2.2   Graphene SQLAlchemy integration
graphql-core        2.2.1   GraphQL implementation for Python
graphql-relay       2.0.0   Relay implementation for Python
graphql-server-core 1.1.1   GraphQL Server tools for powering your server

Yes, I mean one code location for both solutions. Not multiple locations. Only one location uses lru_cache.

This problem is caused by multiple enum classes being created with the same definition. Graphene sees them as different classes with the same name. You can solve it in one of two ways:

  1. by defining the enum class once, then using that defined class at all locations.
  2. use the suggested lru_cache as a wrapper around the class creation to do the same thing

We ran into this problem when creating a custom scalar type and using it in multiple locations. lru_cache wrapping around our returned class solved the problem for us.

That is… really creative! Thanks!