Skip to content
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

enum member가 str이 아닐 경우 에러 #4

Open
item4 opened this issue Mar 11, 2016 · 2 comments
Open

enum member가 str이 아닐 경우 에러 #4

item4 opened this issue Mar 11, 2016 · 2 comments

Comments

@item4
Copy link

item4 commented Mar 11, 2016

tests/reservation_test.py:1: in <module>
    from ***.reservation import Reservation, Traveler, TravelerType
***/reservation.py:83: in <module>
    class Traveler(Base):
***/reservation.py:102: in Traveler
    type = Column(EnumType(TravelerType), nullable=False)
../../.virtualenvs/***/lib/python3.5/site-packages/sqlalchemy_enum34.py:39: in __init__
    super(Enum, self).__init__(*enumerants, **options)
../../.virtualenvs/***/lib/python3.5/site-packages/sqlalchemy/sql/type_api.py:706: in __init__
    self.impl = to_instance(self.__class__.impl, *args, **kwargs)
../../.virtualenvs/***/lib/python3.5/site-packages/sqlalchemy/sql/type_api.py:1162: in to_instance
    return typeobj(*arg, **kw)
../../.virtualenvs/***/lib/python3.5/site-packages/sqlalchemy/sql/sqltypes.py:1135: in __init__
    length = max(len(x) for x in self.enums)
../../.virtualenvs/***/lib/python3.5/site-packages/sqlalchemy/sql/sqltypes.py:1135: in <genexpr>
    length = max(len(x) for x in self.enums)
E   TypeError: object of type 'int' has no len()
class TravelerType(enum.Enum):
    adult = 1
    child = 2
    baby = 3

class Traveler(Base):

    type = Column(EnumType(TravelerType), nullable=False)

아무래도 예외처리를 해줘야 할 것 같습니다.

@edouardberthe
Copy link

I have the same problem. I'm following strictly the documentation:

    from app import db
    from sqlalchemy import Column, String, Enum
    import enum

    class Type(enum.Enum):

        Corp = 'Corp'
        Commodity = 'Comdty'
        Currency = 'Curncy'
        Diff = 'Diff'
        Equity = 'Equity'
        Index = 'Index'

    class Security(db.Model):
        __tablename__ = 'security'

        ticker = Column(String, primary_key=True)
        type = Column(Enum(Type), nullable=False)

And I obtain:

    Config loaded with debug True
    Traceback (most recent call last):
      File "manager.py", line 1, in <module>
        from app import manager
      File "/Users/edouard/Sites/covapp2/app/__init__.py", line 14, in <module>
        from models import *
      File "/Users/edouard/Sites/covapp2/app/models/__init__.py", line 1, in <module>
        from group import Group
      File "/Users/edouard/Sites/covapp2/app/models/group.py", line 7, in <module>
        from security import Security
      File "/Users/edouard/Sites/covapp2/app/models/security.py", line 24, in <module>
        class Security(db.Model):
      File "/Users/edouard/Sites/covapp2/app/models/security.py", line 30, in Security
        type = Column(Enum(Type), nullable=False)
      File "/Users/edouard/Sites/covapp2/env/lib/python2.7/site-packages/sqlalchemy/sql/sqltypes.py", line 1135, in __init__
        length = max(len(x) for x in self.enums)
      File "/Users/edouard/Sites/covapp2/env/lib/python2.7/site-packages/sqlalchemy/sql/sqltypes.py", line 1135, in <genexpr>
        length = max(len(x) for x in self.enums)
    TypeError: object of type 'type' has no len()

@dahlia
Copy link
Contributor

dahlia commented Apr 6, 2016

@edouardberthe I believe the problem of yours differs from @item4’s. You should’ve imported sqlalchemy_enum34.Enum instead of sqlalchemy.Enum.

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

No branches or pull requests

3 participants