NotImplementedError:此表达式不支持运算符“getitem”

Posted

技术标签:

【中文标题】NotImplementedError:此表达式不支持运算符“getitem”【英文标题】:NotImplementedError: Operator 'getitem' is not supported on this expression 【发布时间】:2021-01-19 02:57:41 【问题描述】:

我编写了一个 SQL 查询来为我正在处理的数据库创建一个视图,并且该查询按预期工作:

CREATE OR REPLACE VIEW public.current_model
AS SELECT pm.model_id,
    mp.name,
    ga.pit,
    pm.description,
    pm.created_datetime,
    pm.folder_full_path,
    pm.status
   FROM prediction_model pm
     JOIN material_property mp ON pm.property_id = mp.auto_id
     JOIN geographic_applicability ga ON ga.model_id = pm.auto_id
  WHERE pm.status::text <> 'INACTIVE'::text AND (pm.created_datetime IN ( SELECT max(pm2.created_datetime) AS max
           FROM prediction_model pm2
             JOIN geographic_applicability ga2 ON ga2.model_id = pm2.auto_id
             JOIN material_property mp2 ON pm2.property_id = mp2.auto_id
          GROUP BY mp2.name, ga2.pit));

现在我需要将此视图添加到数据库的 sqlalchemy 架构中。我已经成功实现了其他几个视图,但是这个让我迷失了(请注意,我几乎没有以前的 sqlalchemy 经验)。到目前为止,这是我想出的:

    @classmethod
    def current_model_view(cls):
        j1 = cls.prediction_model.join(cls.material_property,
                                       cls.prediction_model.c.property_id == cls.material_property.c.auto_id)
        j2 = j1.join(cls.geographic_applicability,
                     cls.prediction_model.c.auto_id == cls.geographic_applicability.c.model_id)

        s1 = cls.prediction_model.join(cls.geographic_applicability,
                                       cls.geographic_applicability.c.model_id == cls.prediction_model.c.auto_id)
        s2 = s1.join(cls.material_property, cls.prediction_model.c.property_id == cls.material_property.c.auto_id)

        max_date = select(max(cls.prediction_model.c.created_datetime)).\
            select_from(s2).group_by(cls.material_property.c.name, cls.geographic_applicability.c.pit)

        view_selectable = select([cls.material_property.c.name,
                                  cls.geographic_applicability.c.pit,
                                  cls.prediction_model.c.description,
                                  cls.prediction_model.c.created_datetime,
                                  cls.prediction_model.c.folder_full_path,
                                  cls.prediction_model.c.status]).\
            select_from(j2).\
            where(and_(not_(cls.prediction_model.c.status == 'INACTIVE'),
                       cls.prediction_model.c.created_datetime.in_(max_date)))
        return view_selectable

但这给了我以下错误:

Traceback (most recent call last):
  File "C:/python/mhi_versionist/mhi_versionist/data_base/tracker_database_schema.py", line 320, in <module>
    create_view('current_model', ModelDB.current_model_view(), ModelDB.model_db_meta)
  File "C:/python/mhi_versionist/mhi_versionist/data_base/tracker_database_schema.py", line 154, in current_model_view
    max_date = select(max(cls.prediction_model.c.created_datetime)).\
  File "C:\python\mhi_versionist\venv2\lib\site-packages\sqlalchemy\sql\operators.py", line 432, in __getitem__
    return self.operate(getitem, index)
  File "C:\python\mhi_versionist\venv2\lib\site-packages\sqlalchemy\sql\elements.py", line 762, in operate
    return op(self.comparator, *other, **kwargs)
  File "C:\python\mhi_versionist\venv2\lib\site-packages\sqlalchemy\sql\operators.py", line 432, in __getitem__
    return self.operate(getitem, index)
  File "<string>", line 1, in <lambda>
  File "C:\python\mhi_versionist\venv2\lib\site-packages\sqlalchemy\sql\type_api.py", line 67, in operate
    return o[0](self.expr, op, *(other + o[1:]), **kwargs)
  File "C:\python\mhi_versionist\venv2\lib\site-packages\sqlalchemy\sql\default_comparator.py", line 237, in _getitem_impl
    _unsupported_impl(expr, op, other, **kw)
  File "C:\python\mhi_versionist\venv2\lib\site-packages\sqlalchemy\sql\default_comparator.py", line 241, in _unsupported_impl
    raise NotImplementedError(
NotImplementedError: Operator 'getitem' is not supported on this expression

不知道如何解决。

【问题讨论】:

【参考方案1】:

如下行所示:

max_date = select(max(cls.prediction_model.c.created_datetime)).\

您正在使用 Python 的 max 函数,而不是传递 sql max 函数。您可以尝试使用来自sqlalchemymax 来代替吗?它位于func 模块中。所以写:

max_date = select([func.max(cls.prediction_model.c.created_datetime)]).\

请注意,您还需要在 select 查询中使用列表,因此添加了 [ ]

另一方面,如果ForeignKey 关系已在您的表定义中正确定义,您无需明确告诉sqlalchemy 如何进行连接。因此,在这种情况下,您可以通过以下方式逃脱:

    j1 = cls.prediction_model.join(cls.material_property).join(cls.geographic_applicability)
    s1 = cls.prediction_model.join(cls.geographic_applicability).join(cls.material_property)

【讨论】:

以上是关于NotImplementedError:此表达式不支持运算符“getitem”的主要内容,如果未能解决你的问题,请参考以下文章

NotImplementedError:分配中的无效类型

django storages FTP NotImplementedError(存在方法)

事件的Asyncio NotImplementedError

关于PyTorch继承nn.Module出现raise NotImplementedError的问题解决方案

关于PyTorch继承nn.Module出现raise NotImplementedError的问题解决方案

Python编程中NotImplementedError的使用