graphene-sqlalchemy
graphene-sqlalchemy copied to clipboard
Enum ChoiceType column results in error
Howdy folks!
The following code produces TypeError: 'MyEnum' object is not iterable, but should work. Note that this is also involves package sqlalchemy_utils
from enum import Enum
class MyEnum(Enum):
foo = 1
bar = 2
class MyModel(Base):
id = Column(sa.Integer, primary_key=True)
name = Column(sa.Unicode(255))
type = Column(ChoiceType(MyEnum, impl=Integer()))
class MyObject(SQLAlchemyObjectType):
class Meta:
model = MyModel
Hi !
Is it really a bug ?
The graphene documentation suggest to use graphene.Enum in your schema and, if you already have a defined enum, to use graphene.Enum.from_enum(AlreadyExistingPyEnum)
source: https://docs.graphene-python.org/en/latest/types/enums/
Sure, but that defeats the automagical nature of using SQLAlchemyObjectType along with model = MyModel 😄. Part of the beauty of this library is that I don't have to specify the Schema fields, because they are deduced from the SQLAlchemy model.
There's also the fact that using sqlalchemy.Enum(MyEnum) works but sqlalchemy_utils.ChoiceType(MyEnum) doesn't 😦.
as things stand now, is it not possible to use field = Column(ChoiceType(my_enum))
along with SQLAlchemyObjectType auto models?
is there a workaround ?
Unfortunately I'm no longer on the project that was using this code, so I can't say.
We were able to use a ChoiceType with graphene-sqlalchemy 2.3.0 under Python 3.8 but got this error when we upgraded to Python 3.9. I was able to figure out a fix for it by updating one line in the graphene-sqlalchemy library. If you like I can submit a PR and see if passes review and doesn't break anything else.
@kurtwiersma if you're still up for that, feel free to go ahead. It's probably best to create a new branch for the old release. Would appreciate the effort!