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

Enum ChoiceType column results in error

Open thejcannon opened this issue 6 years ago • 6 comments

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

thejcannon avatar Apr 01 '19 16:04 thejcannon

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/

Nabellaleen avatar Apr 01 '19 17:04 Nabellaleen

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 😦.

thejcannon avatar Apr 01 '19 18:04 thejcannon

as things stand now, is it not possible to use field = Column(ChoiceType(my_enum)) along with SQLAlchemyObjectType auto models?

is there a workaround ?

squarewave24 avatar May 28 '20 13:05 squarewave24

Unfortunately I'm no longer on the project that was using this code, so I can't say.

thejcannon avatar May 28 '20 15:05 thejcannon

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 avatar Oct 29 '21 21:10 kurtwiersma

@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!

erikwrede avatar Aug 12 '22 21:08 erikwrede