如何在SQLAlchemy中的过滤器中使用?

时间:2017-06-30 09:10:09

标签: python sqlalchemy

我有两张表BdiDataLifeCycleBdiSourcePlatform。两个表都有一列event_id。我试图在event_id上加入这两个表格。但它在下面给出错误。当我尝试调用API时。

{
"message": {
"message": "Neither 'InstrumentedAttribute' object nor 'Comparator' object associated with BdiSourcePlatform.event_id has an attribute 'split'"
}
}

event_idBdiDataLifeCycle的PK,但在BdiSourcePlatform event_id中用作csv。 event_id中的BdiSourcePlatform样本值。

1

1,2

4,5,6

5

API:

@app.route("/api/bdi_pf_data", methods=["GET"])
def get_pf_data():
  if request.method == "GET":
    try:
        resp = db.session.query(BdiSourcePlatform,BdiDataLifeCycle)\
                .filter(BdiDataLifeCycle.event_id.in_(BdiSourcePlatform.event_id.split(',')))\
                .filter(BdiSourcePlatform.reference_bpt_template_id == BdiPlatformTemplate.template_id)\
                .values(BdiSourcePlatform.platform_id, BdiSourcePlatform.platform_name, BdiSourcePlatform.platform_type,\
                BdiSourcePlatform.reference_bpt_template_id, BdiDataLifeCycle.event_name,\
                BdiSourcePlatform.db_conn_verified_flg, BdiSourcePlatform.total_tables, BdiSourcePlatform.total_columns,\
                BdiSourcePlatform.data_ingestion_type, BdiSourcePlatform.provider_id, BdiSourcePlatform.transfer_type,\
                BdiSourcePlatform.platform_params, BdiSourcePlatform.event_id, BdiSourcePlatform.parent_platfoms,\
                BdiSourcePlatform.sor_aggregator_flg, BdiSourcePlatform.sor_producer_flg, BdiPlatformTemplate.template_name)
        d = []
        for i in resp:
            x = i.template_name.split('_')[0];
            j = {"platform_id": i.platform_id,"platform_name":i.platform_name,"platform_type":i.platform_type,"reference_bpt_template_id":i.reference_bpt_template_id,\
                "event_name":i.event_name,"db_conn_verified_flg":i.db_conn_verified_flg,"total_tables":i.total_tables,"total_columns":i.total_columns,\
                "data_ingestion_type": i.data_ingestion_type,"provider_id": i.provider_id,"transfer_type": i.transfer_type,"platform_params": i.platform_params,\
                "event_id": i.event_id,"parent_platfoms": i.parent_platfoms,"sor_aggregator_flg": i.sor_aggregator_flg,"sor_producer_flg": i.sor_producer_flg, "template_name":x\
                }
            d.append(j)
        return jsonify({"status":"success","response":d})
    except Exception, e:
        print "exception occured-->"
        print e
        return raiseError(BAD_REQUEST, {"message":str(e)})
  else:
    return jsonify({"status":"success"})

0 个答案:

没有答案