-
Notifications
You must be signed in to change notification settings - Fork 227
Open
Description
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 = Productclass SalableProduct(SQLAlchemyObjectType):
class Meta:
model = SalableProductModel
interfaces = (relay.Node, )
class SalableProductConnections(relay.Connection):
class Meta:
node = SalableProductand 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.
alantrrs, anshajk, adampiskorski and woochica
Activity
Cito commentedon Apr 30, 2019
Is
product_statusan enum?Rafik-Belkadi commentedon Apr 30, 2019
Yes it is in my mixin
Cito commentedon Apr 30, 2019
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 commentedon May 2, 2019
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 ?
Cito commentedon May 2, 2019
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.
rdemetrescu commentedon Jun 13, 2019
I use this monkey-patch on my projects:
allardhoeve commentedon Jun 13, 2019
lungati commentedon Oct 8, 2019
Your code is not in release 2.2.0..
richin13 commentedon Oct 29, 2019
I'm running into this issue. I defined my model like this:
Which fails with:
Versions:
danjenson commentedon Feb 11, 2020
I am also getting this error using the same Enum in various SQLAlchemy models.
danjenson commentedon Feb 11, 2020
So, for the record, I solved this by making global SQLAlchemy types, so @richin13 , your code would become:
spacether commentedon May 14, 2020
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:
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.
rdemetrescu commentedon Jun 20, 2020
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.spacether commentedon Jun 20, 2020
Yes, I mean one code location for both solutions.
Not multiple locations. Only one location uses lru_cache.
lungati commentedon Jan 6, 2021
I notice sqlalchemy_utils enum is being converted to graphene enum type.
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 commentedon Jan 7, 2021
You can turn off the automatic conversion of your enums #98 (comment) This is the solution!!! Spent a day and a half finding it
cglacet commentedon Apr 18, 2021
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.Does crash. While
Works fine. If you have an enum declaration next to your model you can even do:
yesthesoup commentedon Apr 11, 2022
To solve this issue I created a separate type and imported it in both places I want to use it:
and then in the query/mutation:
ddlima-roku commentedon Jul 18, 2022
@rdemetrescu great solution - I hope this can be merged into graphene