首页 > 解决方案 > SQLAlchemy:按 JSON 列中的键过滤

问题描述

SQLAlchemy 版本:1.2.10,PostgreSQL 版本:10-something。
我正在关注此处的文档示例

In [1]: import sqlalchemy as sa

In [2]: from nimble_core.backend.persistence.pg import PG_META_DATA

In [3]: data_table = sa.Table('data_table', PG_META_DATA,
   ...:     sa.Column('id', sa.Integer, primary_key=True),
   ...:     sa.Column('data', sa.JSON)
   ...: )

In [4]: data_table.create()

In [5]: with PG_ENGINE.connect() as conn:
   ...:     conn.execute(
   ...:         data_table.insert(),
   ...:         data = {"key1": "value1", "key2": "value2"}
   ...:     )
   ...:

在哪里:

In [10]: PG_ENGINE
Out[10]: Engine(postgresql://nimble:***@localhost:5432/nimble)

In [11]: PG_META_DATA
Out[11]: MetaData(bind=Engine(postgresql://nimble:***@localhost:5432/nimble))

In [12]: PG_META_DATA.sorted_tables
Out[12]:
[Table('data_table', MetaData(bind=Engine(postgresql://nimble:***@localhost:5432/nimble)), Column('id', Integer(), table=<data_table>, primary_key=True, nullable=False), Column('data', JSON(), table=<data_table>), schema=None)]

插入操作后,表有一行:

    In [14]: PG_ENGINE.execute(sa.select([data_table])).fetchall()
    Out[14]: [(1, {u'key2': u'value2', u'key1': u'value1'})]

我要做的下一件事是按照我的 JSON 列中特定键下的值查询行,如下所示

In [17]: PG_ENGINE.execute(
    ...:     sa.select([data_table]).where(
    ...:         data_table.c.data['key1'].astext == 'value1'
    ...:     )
    ...: )
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-17-3c4db8afed3f> in <module>()
      1 PG_ENGINE.execute(
      2     sa.select([data_table]).where(
----> 3         data_table.c.data['key1'].astext == 'value1'
      4     )
      5 )

/Users/psih/Work/nimble-server/runtime/lib/python2.7/site-packages/sqlalchemy/sql/elements.pyc in __getattr__(self, key)
    686                     type(self).__name__,
    687                     type(self.comparator).__name__,
--> 688                     key)
    689             )
    690

AttributeError: Neither 'BinaryExpression' object nor 'Comparator' object has an attribute 'astext'

显然 a 的类型data_table.c.data['key1']是 something ( sqlalchemy.sql.elements.BinaryExpression) ,它没有属性astext。这是否意味着文档有误?

标签: pythonpostgresqlsqlalchemy

解决方案


您使用的sqlalchemy.types.JSON没有astext. 改用sqlalchemy.dialects.postgresql.JSON _

import sqlalchemy as sa
from sqlalchemy.dialects import postgresql

data_table = sa.Table('data_table', PG_META_DATA,
    sa.Column('id', sa.Integer, primary_key=True),
    sa.Column('data', postgresql.JSON)
)

推荐阅读