首页 > 解决方案 > SQLAlchemy:用存储过程或更复杂的 sql 语句填充对象

问题描述

我有一个名为 Feature 的 sql 炼金术类。

class Feature(DBConnect.Base):
    __tablename__ = 'feature'
    id = Column(Integer, primary_key=True)
    name = Column(String(500), nullable=False, default='')

在其他代码中,我可以通过执行以下操作获得功能:

feature = db.session.query(Feature).get(10)

我最终得到了一个特征对象。是否可以执行存储过程或更复杂的查询,如下所示:

select * from feature f
join aligned_data_column a on a.id=f.aligned_Data_column_id and 
a.aligned_data_id=76
where a.aligned_data_id=70 
union
select * from feature f
join aligned_data_column a on a.aligned_data_id=76
where json_contains(json_extract(f.column_mappings, '$.*'),CAST(a.id as JSON))

我希望能够将该查询输入到存储过程或代码中的某个位置。我正在使用 MySQL 和 python 3.x

标签: pythonmysqlsqlalchemy

解决方案


推荐阅读