flask-sqlalchemy with_entities和关系 [英] flask-sqlalchemy with_entities and relationships
本文介绍了flask-sqlalchemy with_entities和关系的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我只想从模型中选择几列来加快查询速度,但是我要选择的一列来自关系.
I want to select only a couple columns from my model to speed up the queries, but one of the columns I want to select is from a relationship.
型号:
class OpenChromatinRegion(db.Model):
...
gene_id = db.Column(db.Integer, db.ForeignKey("gene.id"), nullable=False, index=True)
gene = db.relationship("Gene", back_populates='open_chromatin_regions')
class Gene(db.Model):
id = db.Column(db.Integer, primary_key=True)
ENSEMBLID = db.Column(db.Integer, index=True, unique=True, nullable=False)
...
查询:
q = OpenChromatinRegion.query.with_entities(Gene.ENSEMBLID, ...)...
我如何使用我以前尝试过的.with_entities(OpenChromatinRegion.gene.ENSEMBLID)
的flask-sqlalchemy正确地从OpenChromatinRegion中仅选择几列,但是那也不起作用.使用这种语法,我没有收到错误,但是请求超时.
How do I properly select only a couple columns from OpenChromatinRegion using flask-sqlalchemy I previously tried .with_entities(OpenChromatinRegion.gene.ENSEMBLID)
but that didn't work either. With this syntax, I don't get an error but the request times out.
推荐答案
您需要进行加入:
q = OpenChromatinRegion.query.join(OpenChromatinRegion.gene) \
.with_entities(Gene.ENSEMBLID)
这篇关于flask-sqlalchemy with_entities和关系的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文