SQLAlchemy:使用另一个查询的结果从 table 中查找所有匹配的行
SQLAlchemy: Finding all matching rows from a table with results from another query
我有四个table:
- 页
- wikiPageID
- url
- Collection
- collectionID
- wikiPageID
- PageReference(加入table)
- wikiPageID
- 参考编号
- 参考
- 参考编号
对于任何给定的 URL 对应于 page
table 中的一行,我想 return 将所有引用分配给可能属于的任何同级页面同样的 collection.
我觉得可能有更有效的方法来做到这一点。到目前为止,我已将其分解为一组查询,并且只完成了一半。
# Get all ReferenceData from Reference table for ID
wikiPageID = ""
url = request.json['url']
# Get WikiPageID from Page table where Page.url = request.json['url']
for item in db.session.query(PageModel).filter(PageModel.url == url).values('wikiPageID'):
wikiPageID = item[0]
#print(wikiPageID)
for item in
db.session.query(CollectionModel).filter(CollectionModel.wikiPageID == wikiPageID).values('collectionID'):
collectionID = item[0]
#print(collectionID)
# Get all WikiPageID's from Collection table where WikiPageID EXISTS
result1 = db.session.query(CollectionModel).filter(CollectionModel.collectionID == collectionID).values('wikiPageID')
## THIS IS WHERE IT STARTS GOING PEAR SHAPED
# Get all ReferenceID from PageReference where WikiPageID EXISTS
result = db.session.query(PageReferenceModel).filter(PageReferenceModel.wikiPageID.in_(result1) )
schema = CollectionSchema()
return schema.dump(result, many=True), 200
在我的最后一个查询中,我试图嵌套以前的查询结果。当我试图通过从上一个结果传递的 ID 过滤结果时。我觉得我做的这一切都是错误的,无法确定从这里去哪里。
SQLAlchemy 模型
class CollectionModel(db.Model):
__tablename__ = "Collection"
collectionID = db.Column(db.Integer, primary_key=True)
wikiPageID = db.Column(db.Integer, db.ForeignKey('Page.wikiPageID'), nullable=False)
Page = db.relationship("PageModel")
class PageModel(db.Model):
__tablename__ = "Page"
wikiPageID = db.Column(db.Integer, primary_key=True)
langCode = db.Column(db.String, nullable=True)
title = db.Column(db.String, nullable=True)
url = db.Column(db.String, nullable=True)
summary = db.Column(db.String, nullable=True)
lastUpdated = db.Column(db.String, nullable=True)
class PageReferenceModel(db.Model):
__tablename__ = "PageReference"
pageReferenceID = db.Column(db.Integer, primary_key=True)
wikiPageID = db.Column(db.Integer, db.ForeignKey('Page.wikiPageID'), nullable=False)
referenceID = db.Column(db.Integer, db.ForeignKey('Reference.referenceID'), nullable=False)
Page = db.relationship("PageModel")
Reference = db.relationship("ReferenceModel")
class ReferenceModel(db.Model):
__tablename__ = "Reference"
referenceID = db.Column(db.Integer, primary_key=True)
langCode = db.Column(db.String, nullable=False)
isGov = db.Column(db.String, nullable=False)
type = db.Column(db.String, nullable=True)
title = db.Column(db.String, nullable=True)
domain = db.Column(db.String, nullable=True)
suffix = db.Column(db.String, nullable=True)
description = db.Column(db.String, nullable=True)
publishDate = db.Column(db.String, nullable=True)
url = db.Column(db.String, nullable=True)
youtubeId = db.Column(db.String, nullable=True)
langCheck = db.Column(db.Boolean, nullable=True)
已修复:
我最终对先前查询的结果做了一系列 if 语句。仍然很可能效率低下。但它有效。
Collection 得到
# Get all ReferenceData from Reference table for ID
wikiPageID = ""
url = request.json['url']
# Get all ReferenceData from Reference table for ID
wikiPageID = ""
url = request.json['url']
# Get WikiPageID from Page table where Page.url = request.json['url']
for item in db.session.query(PageModel).filter(PageModel.url == url).values(PageModel.wikiPageID):
wikiPageID = item[0]
#print(wikiPageID)
for item in db.session.query(CollectionModel).filter(CollectionModel.wikiPageID == wikiPageID).values(CollectionModel.collectionID):
collectionID = item[0]
#print(collectionID)
# Get all WikiPageID's from Collection table where WikiPageID EXISTS
wikiPageIDResult = db.session.query(CollectionModel).filter(CollectionModel.collectionID == collectionID).values(CollectionModel.wikiPageID)
wikiPageArray = []
for item in wikiPageIDResult:
wikiPageArray.append(item[0])
print("Wiki Page Array: ", wikiPageArray)
#Get all ReferenceID from PageReference where WikiPageID EXISTS
wikiPageReferenceResults = db.session.query(PageReferenceModel).filter(PageReferenceModel.wikiPageID.in_(wikiPageArray)).values(PageReferenceModel.referenceID)
wikiPageReferenceArray = []
for item in wikiPageReferenceResults:
wikiPageReferenceArray.append(item[0])
print("Wiki Page Reference Array: ", wikiPageReferenceArray)
referenceResult = db.session.query(ReferenceModel).filter(ReferenceModel.referenceID.in_(wikiPageReferenceArray))
schema = ReferenceSchema()
return schema.dump(referenceResult, many=True), 200
你应该使用连接:
result = db.session.query(PageReferenceModel)\
.join(CollectionModel, CollectionModel.wikiPageId == PageReferenceModel.wikiPageId)\
.join(PageModel, PageModel.wikiPageId == PageReferenceModel.wikiPageId)\
.filter(PageModel.url == url)
schema = CollectionSchema()
return schema.dump(result, many=True), 200
我有四个table:
- 页
- wikiPageID
- url
- Collection
- collectionID
- wikiPageID
- PageReference(加入table)
- wikiPageID
- 参考编号
- 参考
- 参考编号
对于任何给定的 URL 对应于 page
table 中的一行,我想 return 将所有引用分配给可能属于的任何同级页面同样的 collection.
我觉得可能有更有效的方法来做到这一点。到目前为止,我已将其分解为一组查询,并且只完成了一半。
# Get all ReferenceData from Reference table for ID
wikiPageID = ""
url = request.json['url']
# Get WikiPageID from Page table where Page.url = request.json['url']
for item in db.session.query(PageModel).filter(PageModel.url == url).values('wikiPageID'):
wikiPageID = item[0]
#print(wikiPageID)
for item in
db.session.query(CollectionModel).filter(CollectionModel.wikiPageID == wikiPageID).values('collectionID'):
collectionID = item[0]
#print(collectionID)
# Get all WikiPageID's from Collection table where WikiPageID EXISTS
result1 = db.session.query(CollectionModel).filter(CollectionModel.collectionID == collectionID).values('wikiPageID')
## THIS IS WHERE IT STARTS GOING PEAR SHAPED
# Get all ReferenceID from PageReference where WikiPageID EXISTS
result = db.session.query(PageReferenceModel).filter(PageReferenceModel.wikiPageID.in_(result1) )
schema = CollectionSchema()
return schema.dump(result, many=True), 200
在我的最后一个查询中,我试图嵌套以前的查询结果。当我试图通过从上一个结果传递的 ID 过滤结果时。我觉得我做的这一切都是错误的,无法确定从这里去哪里。
SQLAlchemy 模型
class CollectionModel(db.Model):
__tablename__ = "Collection"
collectionID = db.Column(db.Integer, primary_key=True)
wikiPageID = db.Column(db.Integer, db.ForeignKey('Page.wikiPageID'), nullable=False)
Page = db.relationship("PageModel")
class PageModel(db.Model):
__tablename__ = "Page"
wikiPageID = db.Column(db.Integer, primary_key=True)
langCode = db.Column(db.String, nullable=True)
title = db.Column(db.String, nullable=True)
url = db.Column(db.String, nullable=True)
summary = db.Column(db.String, nullable=True)
lastUpdated = db.Column(db.String, nullable=True)
class PageReferenceModel(db.Model):
__tablename__ = "PageReference"
pageReferenceID = db.Column(db.Integer, primary_key=True)
wikiPageID = db.Column(db.Integer, db.ForeignKey('Page.wikiPageID'), nullable=False)
referenceID = db.Column(db.Integer, db.ForeignKey('Reference.referenceID'), nullable=False)
Page = db.relationship("PageModel")
Reference = db.relationship("ReferenceModel")
class ReferenceModel(db.Model):
__tablename__ = "Reference"
referenceID = db.Column(db.Integer, primary_key=True)
langCode = db.Column(db.String, nullable=False)
isGov = db.Column(db.String, nullable=False)
type = db.Column(db.String, nullable=True)
title = db.Column(db.String, nullable=True)
domain = db.Column(db.String, nullable=True)
suffix = db.Column(db.String, nullable=True)
description = db.Column(db.String, nullable=True)
publishDate = db.Column(db.String, nullable=True)
url = db.Column(db.String, nullable=True)
youtubeId = db.Column(db.String, nullable=True)
langCheck = db.Column(db.Boolean, nullable=True)
已修复:
我最终对先前查询的结果做了一系列 if 语句。仍然很可能效率低下。但它有效。
Collection 得到
# Get all ReferenceData from Reference table for ID
wikiPageID = ""
url = request.json['url']
# Get all ReferenceData from Reference table for ID
wikiPageID = ""
url = request.json['url']
# Get WikiPageID from Page table where Page.url = request.json['url']
for item in db.session.query(PageModel).filter(PageModel.url == url).values(PageModel.wikiPageID):
wikiPageID = item[0]
#print(wikiPageID)
for item in db.session.query(CollectionModel).filter(CollectionModel.wikiPageID == wikiPageID).values(CollectionModel.collectionID):
collectionID = item[0]
#print(collectionID)
# Get all WikiPageID's from Collection table where WikiPageID EXISTS
wikiPageIDResult = db.session.query(CollectionModel).filter(CollectionModel.collectionID == collectionID).values(CollectionModel.wikiPageID)
wikiPageArray = []
for item in wikiPageIDResult:
wikiPageArray.append(item[0])
print("Wiki Page Array: ", wikiPageArray)
#Get all ReferenceID from PageReference where WikiPageID EXISTS
wikiPageReferenceResults = db.session.query(PageReferenceModel).filter(PageReferenceModel.wikiPageID.in_(wikiPageArray)).values(PageReferenceModel.referenceID)
wikiPageReferenceArray = []
for item in wikiPageReferenceResults:
wikiPageReferenceArray.append(item[0])
print("Wiki Page Reference Array: ", wikiPageReferenceArray)
referenceResult = db.session.query(ReferenceModel).filter(ReferenceModel.referenceID.in_(wikiPageReferenceArray))
schema = ReferenceSchema()
return schema.dump(referenceResult, many=True), 200
你应该使用连接:
result = db.session.query(PageReferenceModel)\
.join(CollectionModel, CollectionModel.wikiPageId == PageReferenceModel.wikiPageId)\
.join(PageModel, PageModel.wikiPageId == PageReferenceModel.wikiPageId)\
.filter(PageModel.url == url)
schema = CollectionSchema()
return schema.dump(result, many=True), 200