在 SQLAlchemy 中区分 RowProxy 的 json 列?
distinguishing json column of RowProxy in SQLAlchemy?
例如,当我执行下面的代码时,结果数据类型是str
:
result = engine.execute('''
SELECT CAST('{"foo": "bar"}' as JSON) as `json`
''')
row = result.fetchone()
json = row[0]
type(json)
具有 str
类型的 json 列值对元编程不太友好。
问题
是否有任何方法可以从 result
(或 ResultProxy
的实例)中获取每列类型的信息?
环境
- MySQL: 8.0.11
- SQLAlchemy: 1.3.0
- pymysql: 0.9.3
你至少可以明确地实现它 telling SQLAlchemy that the result is JSON:
from sqlalchemy.types import JSON
stmt = text('''SELECT CAST('{"foo": "bar"}' as JSON) as `json`''')
stmt = stmt.columns(json=JSON)
row = engine.execute(stmt).fetchone()
type(row.json)
例如,当我执行下面的代码时,结果数据类型是str
:
result = engine.execute('''
SELECT CAST('{"foo": "bar"}' as JSON) as `json`
''')
row = result.fetchone()
json = row[0]
type(json)
具有 str
类型的 json 列值对元编程不太友好。
问题
是否有任何方法可以从 result
(或 ResultProxy
的实例)中获取每列类型的信息?
环境
- MySQL: 8.0.11
- SQLAlchemy: 1.3.0
- pymysql: 0.9.3
你至少可以明确地实现它 telling SQLAlchemy that the result is JSON:
from sqlalchemy.types import JSON
stmt = text('''SELECT CAST('{"foo": "bar"}' as JSON) as `json`''')
stmt = stmt.columns(json=JSON)
row = engine.execute(stmt).fetchone()
type(row.json)