Skip to content

Enum ChoiceType column results in error #196

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
thejcannon opened this issue Apr 1, 2019 · 6 comments
Open

Enum ChoiceType column results in error #196

thejcannon opened this issue Apr 1, 2019 · 6 comments
Labels

Comments

@thejcannon
Copy link

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 pushed a commit to thejcannon/graphene-sqlalchemy that referenced this issue Apr 1, 2019
thejcannon pushed a commit to thejcannon/graphene-sqlalchemy that referenced this issue Apr 1, 2019
@Nabellaleen
Copy link
Collaborator

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/

@thejcannon
Copy link
Author

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

@squarewave24
Copy link

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

is there a workaround ?

@thejcannon
Copy link
Author

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

@kurtwiersma
Copy link

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.

@erikwrede
Copy link
Member

erikwrede commented Aug 12, 2022

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants