spoqa/sqlalchemy-enum34

enum member가 str이 아닐 경우 에러

item4 opened this issue · 2 comments

item4 commented
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)

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

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()

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