SQLAlchemy在PostgreSQL JSONB中过滤嵌套的JSON数据

Nig*_*uki 5 python postgresql json sqlalchemy

我将嵌套JSON存储为jsonb,但我不知道如何选择具有不确定值的嵌套json.

例如:

{
    "facebook": {
        "openid": "123456789",
        "access_token": "6EFD26B0A868E3BB387E78851E42943F"
    }
}
Run Code Online (Sandbox Code Playgroud)

我知道openid的值,但access_token是不确定的.

我尝试了以下但它引发了一个错误.

cls.query.filter(User.auth["facebook"]["openid"].astext == openid).first()
Run Code Online (Sandbox Code Playgroud)

hmn*_*ahi 0

使用这种方法来过滤嵌套的 jsonb 列:

user = session.query(User) \
    .filter(text("CAST(auth->'facebook'->>'openid' AS TEXT) = :openid")) \
    .params(openid=openid) \
    .first()
Run Code Online (Sandbox Code Playgroud)