SQLAlchemy 中的多个连接经常失败 'sqlalchemy.exc.ArgumentError'
Multiple joins in SQLAlchemy often fails with 'sqlalchemy.exc.ArgumentError'
我不确定如何使用 SQLalchemy 正确地进行多重连接。
我正在尝试从 get 请求中获得一个结果,该结果提供了一个叙述列表 NarrativeModel
,其中包含一个或多个引用 ReferenceModel
,其中 publishDate
是一个给定值.在下面的示例中,这是 2022-03-09
但是,我不知道如何在 Narrative
、NarrativeReference
和 Reference
之间执行联接。我的尝试大多以以下或类似结尾:sqlalchemy.exc.ArgumentError: SQL expression object expected, got object of type <class 'flask_sqlalchemy.model.DefaultMeta'> instead
叙事模型
class NarrativeModel(db.Model):
__tablename__ = "narrative"
id = db.Column(db.Integer, primary_key=True)
title = db.Column(db.String, nullable=False)
date = db.Column(db.String, nullable=True)
ownerProfileID = db.Column(db.Integer, db.ForeignKey('profile.id'), nullable=False)
scheduled = db.Column(db.Integer, nullable=True)
ongoing = db.Column(db.Integer, nullable=True)
NarrativeReference 模型(加入table)
class NarrativeReferenceModel(db.Model):
__tablename__ = "narrativereference"
id = db.Column(db.Integer, primary_key=True)
narrativeID = db.Column(db.Integer, db.ForeignKey('narrative.id'), nullable=False)
referenceID = db.Column(db.Integer, db.ForeignKey('reference.id'), nullable=False)
参考模型
class ReferenceModel(db.Model):
__tablename__ = "reference"
id = db.Column(db.Integer, primary_key=True)
langCode= db.Column(db.String, nullable=True)
isGov = db.Column(db.String, nullable=True)
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)
addedDate = db.Column(db.String, nullable=True)
url = db.Column(db.String, nullable=False)
youtubeId = db.Column(db.String, nullable=True)
langCheck = db.Column(db.Integer, nullable=True)
获取请求
class GlobalNarrativesByTodaysUpdates (Resource):
#RETURNS ALL AVAILABLE NARRATIVES
# Called with: curl --header "Content-Type: application/json" --request GET http://localhost:8080/narratives
def get(self):
schema = NarrativeSchema()
result = NarrativeModel.query\
.join(NarrativeReferenceModel, NarrativeModel.id == NarrativeReferenceModel.narrativeID)\
.filter(ReferenceModel, ReferenceModel.publishDate == '2022-03-09').all()
db.session.close()
return schema.dump(result, many=True), 200
满足我需求的有效 SQL 语句是:
select distinct narrative.title, narrative.date from narrative, narrativereference
where narrativereference.narrativeID = narrative.id
and narrativereference.referenceID in (select id from reference where publishDate = '2022-03-09')
由于关系已在模型中定义,因此使用简单连接修复了它
def get(self):
schema = NarrativeSchema()
result = NarrativeModel.query\
.join(NarrativeReferenceModel)\
.join(ReferenceModel)\
.filter(ReferenceModel.addedDate == '2022-03-09').all()
db.session.close()
return schema.dump(result, many=True), 200
我不确定如何使用 SQLalchemy 正确地进行多重连接。
我正在尝试从 get 请求中获得一个结果,该结果提供了一个叙述列表 NarrativeModel
,其中包含一个或多个引用 ReferenceModel
,其中 publishDate
是一个给定值.在下面的示例中,这是 2022-03-09
但是,我不知道如何在 Narrative
、NarrativeReference
和 Reference
之间执行联接。我的尝试大多以以下或类似结尾:sqlalchemy.exc.ArgumentError: SQL expression object expected, got object of type <class 'flask_sqlalchemy.model.DefaultMeta'> instead
叙事模型
class NarrativeModel(db.Model):
__tablename__ = "narrative"
id = db.Column(db.Integer, primary_key=True)
title = db.Column(db.String, nullable=False)
date = db.Column(db.String, nullable=True)
ownerProfileID = db.Column(db.Integer, db.ForeignKey('profile.id'), nullable=False)
scheduled = db.Column(db.Integer, nullable=True)
ongoing = db.Column(db.Integer, nullable=True)
NarrativeReference 模型(加入table)
class NarrativeReferenceModel(db.Model):
__tablename__ = "narrativereference"
id = db.Column(db.Integer, primary_key=True)
narrativeID = db.Column(db.Integer, db.ForeignKey('narrative.id'), nullable=False)
referenceID = db.Column(db.Integer, db.ForeignKey('reference.id'), nullable=False)
参考模型
class ReferenceModel(db.Model):
__tablename__ = "reference"
id = db.Column(db.Integer, primary_key=True)
langCode= db.Column(db.String, nullable=True)
isGov = db.Column(db.String, nullable=True)
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)
addedDate = db.Column(db.String, nullable=True)
url = db.Column(db.String, nullable=False)
youtubeId = db.Column(db.String, nullable=True)
langCheck = db.Column(db.Integer, nullable=True)
获取请求
class GlobalNarrativesByTodaysUpdates (Resource):
#RETURNS ALL AVAILABLE NARRATIVES
# Called with: curl --header "Content-Type: application/json" --request GET http://localhost:8080/narratives
def get(self):
schema = NarrativeSchema()
result = NarrativeModel.query\
.join(NarrativeReferenceModel, NarrativeModel.id == NarrativeReferenceModel.narrativeID)\
.filter(ReferenceModel, ReferenceModel.publishDate == '2022-03-09').all()
db.session.close()
return schema.dump(result, many=True), 200
满足我需求的有效 SQL 语句是:
select distinct narrative.title, narrative.date from narrative, narrativereference
where narrativereference.narrativeID = narrative.id
and narrativereference.referenceID in (select id from reference where publishDate = '2022-03-09')
由于关系已在模型中定义,因此使用简单连接修复了它
def get(self):
schema = NarrativeSchema()
result = NarrativeModel.query\
.join(NarrativeReferenceModel)\
.join(ReferenceModel)\
.filter(ReferenceModel.addedDate == '2022-03-09').all()
db.session.close()
return schema.dump(result, many=True), 200