graphene-sqlalchemy icon indicating copy to clipboard operation
graphene-sqlalchemy copied to clipboard

AssertionError: Found different types with the same name in the schema

Open Rafik-Belkadi opened this issue 6 years ago • 19 comments

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.

Rafik-Belkadi avatar Apr 29 '19 11:04 Rafik-Belkadi

Is product_status an enum?

Cito avatar Apr 30 '19 13:04 Cito

Is product_status an enum?

Yes it is in my mixin

Rafik-Belkadi avatar Apr 30 '19 13:04 Rafik-Belkadi

Then it's the known problem for which I have proposed a solution in #210. It happens when you use the same enum in different columns (or maybe in your case, in a class and a subclass). The enum type is not reused, but created twice with the same name.

Cito avatar Apr 30 '19 16:04 Cito

I really could not get to apply your solutions, could you please provide some details, saw your latest commits on improving Enum type creation but can't use it at the moment. is there a quick fix i can try ?

Then it's the known problem for which I have proposed a solution in #210. It happens when you use the same enum in different columns (or maybe in your case, in a class and a subclass). The enum type is not reused, but created twice with the same name.

Rafik-Belkadi avatar May 02 '19 12:05 Rafik-Belkadi

You would probably need to somehow patch the SalableProduct to make it use the same enum, when using the current version of graphene-sqlalchemy. I expect the fix will be merged and released soon.

Cito avatar May 02 '19 17:05 Cito

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)

rdemetrescu avatar Jun 13 '19 00:06 rdemetrescu

That is... really creative! Thanks!

allardhoeve avatar Jun 13 '19 06:06 allardhoeve

Your code is not in release 2.2.0..

lungati avatar Oct 08 '19 08:10 lungati

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

richin13 avatar Oct 29 '19 17:10 richin13

I am also getting this error using the same Enum in various SQLAlchemy models.

danjenson avatar Feb 11 '20 01:02 danjenson

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)

danjenson avatar Feb 11 '20 03:02 danjenson

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.

spacether avatar May 14 '20 06:05 spacether

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.

when you say "wrapping around our returned class" you don't mean you are using the lru_cache multiple times in your project, right?

The lru_cache hack I suggested should be used only once before your SQLAlchemy models are declared. If your model declarations are spread among multiple files, you don't need to write graphene.Enum.from_enum = lru_cache(maxsize=None)(graphene.Enum.from_enum) on each file.

rdemetrescu avatar Jun 20 '20 00:06 rdemetrescu

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

spacether avatar Jun 20 '20 01:06 spacether

I notice sqlalchemy_utils enum is being converted to graphene enum type.

    1. This section of code is triggered multiple times: def from_enum( cls, enum, description=None, deprecation_reason=None ): # noqa: N805 description = description or enum.__doc__ meta_dict = { "enum": enum, "description": description, "deprecation_reason": deprecation_reason, } meta_class = type("Meta", (object,), meta_dict) return type(meta_class.enum.__name__, (Enum,), {"Meta": meta_class})

-2 Here.. Not sure if this is triggered automatically: @convert_sqlalchemy_type.register(types.Enum) def convert_enum_to_enum(type, column, registry=None): return lambda: enum_for_sa_enum(type, registry or get_global_registry())

N.B: My models include this section: business_domain = Column(Enum(BusinessDomain), nullable=False)

-3 And here... I trigger the same code here because I need to list the values of the enum. Commenting out this section resolves the issue but I need this list of values! graphene.List(graphene.Enum.from_enum(BusinessDomain))

Noticed the LRU cache fix above won't help if you have different enums

lungati avatar Jan 06 '21 12:01 lungati

You can turn off the automatic conversion of your enums https://github.com/graphql-python/graphene-sqlalchemy/pull/98#issuecomment-481497115 This is the solution!!! Spent a day and a half finding it

lungati avatar Jan 07 '21 07:01 lungati

I feel like sqlalchemy is building things every time it reads Enum(e). So if we use two columns with this type it will fail the second time.

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

Does crash. While


StatusColumn = db.Enum(AdStatus)

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

Works fine. If you have an enum declaration next to your model you can even do:

import enum
from sqlalchemy import Column, Enum

@Enum
class Status(enum.Enum):
    STARTED = 1
    CANCELED = 2
    
   
class TagType(Base):
    status_before = Column(Status, nullable=False)
    status_during = Column(Status, nullable=False)

cglacet avatar Apr 18 '21 13:04 cglacet

To solve this issue I created a separate type and imported it in both places I want to use it:

import graphene

from my_project.models.core import MyEnum

my_enum = graphene.Enum.from_enum(MyEnum)

and then in the query/mutation:

from my_project.routes.graphql.types.common import my_enum

class MyMutation(graphene.Mutation)

    ...

    class Arguments:
       ...
       paramName = my_enum(required=True)
       ...

yesthesoup avatar Apr 11 '22 17:04 yesthesoup

@rdemetrescu great solution - I hope this can be merged into graphene

ddlima-roku avatar Jul 18 '22 21:07 ddlima-roku