zoukankan      html  css  js  c++  java
  • query data filtered by a JSON Column in SQLAlchemy

    https://stackoverflow.com/questions/27288903/how-can-i-query-data-filtered-by-a-json-column-in-sqlalchemy

    from sqlalchemy.dialects.postgresql import JSON
    from flask.ext.sqlalchemy import SQLAlchemy

    ...

    db = SQLAlchemy()

    ...

    class Custom(db.Model):
        __tablename__ = 'custom'
        ...
        data = db.Column(JSON)
        ...

    data field are like this: [{"type": "a string", "value": "value string"}, {"type": "another", "value": "val"}, ...]

    I want to query all custom objects that their data contain a {"type": "anything", "value": "what I want"}.

    ##########################answer##################################

    from sqlalchemy.types import Unicode
    # codes...
    Custom.query.filter(Custom.data['value'].astext.cast(Unicode) == "what I want")

    Documentation for this can be found here: http://docs.sqlalchemy.org/en/latest/dialects/postgresql.html#sqlalchemy.dialects.postgresql.JSON

  • 相关阅读:
    nginx特性
    mysql增删改查
    keepalived+nginx集群
    nginx+tomcat集群方法
    jdk环境配置(Windows)
    nginx_http核心模块(二)
    nginx入门手册(一)
    nginx 配置文件解析(一)
    tcpdump用法
    Tcpdump.
  • 原文地址:https://www.cnblogs.com/binzhou75/p/12027688.html
Copyright © 2011-2022 走看看