Sqlalchemy raise ‘NotImplementedError: Operator ‘getitem’ is not supported on this expression’ When use ‘hybrid_property’ in mysql.SET

Hello everyone:

I success to create ‘user’ table, but failed to add ‘user’ to table.

python version: 3.7.2

SQLAlchemy 1.3.18

  • create permission enum
from sqlalchemy.dialects.mysql import SET
from enum import Enum
class UserPermission(Enum):
    A = auto()
    B = auto()
  • declare ‘user’ table
class User(Base):
    __tablename__ = 'user'
    _permission = Column('permission', SET(*[name for name,value in UserPermission.__members__.items()]), comment = "user permission", nullable = False, default=set())
    @hybrid_property
    def permission(self):
        '''
        convert 'str' set to 'Enum' set
        '''
        permissionEnumSet = set()
        for element in self._permission:
            permissionEnumSet.add(UserPermission[element])
        return permissionEnumSet

    @permission.setter
    def permission(self, permissionEnumSet):
        '''
        convert 'Enum' set to 'str' set
        ''' 
        self._permission = [permission.name for permission in permissionEnumSet]
  • add one ‘user’ to table
with Session() as session:
    user = User(permission = set([UserPermission.A])) # it raise 'NotImplementedError: Operator 'getitem' is not supported on this expression'

  • raise exception
File "./src/model.py", line 87, in permission
    for element in self._permission:
  File "/root/.pyenv/versions/3.7.2/lib/python3.7/site-packages/sqlalchemy/sql/operators.py", line 432, in __getitem__
    return self.operate(getitem, index)
  File "/root/.pyenv/versions/3.7.2/lib/python3.7/site-packages/sqlalchemy/orm/attributes.py", line 219, in operate
    return op(self.comparator, *other, **kwargs)
  File "/root/.pyenv/versions/3.7.2/lib/python3.7/site-packages/sqlalchemy/sql/operators.py", line 432, in __getitem__
    return self.operate(getitem, index)
  File "/root/.pyenv/versions/3.7.2/lib/python3.7/site-packages/sqlalchemy/orm/properties.py", line 367, in operate
    return op(self.__clause_element__(), *other, **kwargs)
  File "/root/.pyenv/versions/3.7.2/lib/python3.7/site-packages/sqlalchemy/sql/operators.py", line 432, in __getitem__
    return self.operate(getitem, index)
  File "/root/.pyenv/versions/3.7.2/lib/python3.7/site-packages/sqlalchemy/sql/elements.py", line 759, in operate
    return op(self.comparator, *other, **kwargs)
  File "/root/.pyenv/versions/3.7.2/lib/python3.7/site-packages/sqlalchemy/sql/operators.py", line 432, in __getitem__
    return self.operate(getitem, index)
  File "<string>", line 1, in <lambda>
  File "/root/.pyenv/versions/3.7.2/lib/python3.7/site-packages/sqlalchemy/sql/type_api.py", line 67, in operate
    return o[0](self.expr, op, *(other + o[1:]), **kwargs)
  File "/root/.pyenv/versions/3.7.2/lib/python3.7/site-packages/sqlalchemy/sql/default_comparator.py", line 237, in _getitem_impl
    _unsupported_impl(expr, op, other, **kw)
  File "/root/.pyenv/versions/3.7.2/lib/python3.7/site-packages/sqlalchemy/sql/default_comparator.py", line 242, in _unsupported_impl
    "Operator '%s' is not supported on " "this expression" % op.__name__
NotImplementedError: Operator 'getitem' is not supported on this expression

Do you have any good ideas?

Answer

I think it has been instantiated to be an ‘my.SET’ object before assign so that ‘hybird_property’ is called. The object likes ‘None’, it can’t be Iterated. I use below code to check it.

    from collections.abc import Iterable

    @hybrid_property
    def permission(self):
        permissionEnumSet = set()
        if isinstance(self._permission ,Iterable):
            for element in self._permission:
                permissionEnumSet.add(UserPermission[element])
        return permissionEnumSet

It works. Do you think it’s okay to do this?