使用 SQLAlchemy 连接多个表
我想我已经很接近了,但也许我需要一些指导。我正在尝试在 SQL Alchemy 中运行下面的 SQL 查询。
Endpoints for populating the UI with data. These need to consist of some joins.
select
constraintid,
sja.segment,
sjs.source,
sjct.constrainttype,
constraintvalue,
targeting,
frequency,
period
from signaljourneyaudienceconstraints
JOIN signaljourneyaudiences sja ON sja.audienceid = signaljourneyaudienceconstraints.audienceid;
JOIN signaljourneysources sjs ON sjs.sourceid = signaljourneyaudienceconstraints.sourceid
JOIN signaljourneyconstrainttype sjct ON sjct.constrainttypeid = signaljourneyaudienceconstraints.constrainttypeid
我读过一些文档,我认为我的思路是正确的,但我不断收到这样的错误
sqlalchemy.exc.ArgumentError: Join target SignalJourneyAudiences.segment does not refer to a mapped entity
,因为 SignalJourneyAudiences.segment
不是关键,我试图复制我知道的 SQL 查询作品。
@app.get("/get-main-query-data", status_code=status.HTTP_200_OK)
def get_main_query_data():
"""Returns data for the main query."""
return (
db.query(
models.SignalJourneyAudienceConstraints.constraintId,
models.SignalJourneyAudiences.segment,
models.SingalJourneySources.source,
models.SignalJourneyConstraintType.constraintType,
models.SignalJourneyAudienceConstraints.constraintValue,
models.SignalJourneyAudienceConstraints.targeting,
models.SignalJourneyAudienceConstraints.frequency,
models.SignalJourneyAudienceConstraints.period,
)
.join(
models.SignalJourneyAudiences.segment,
models.SignalJourneyAudiences.audienceId
== models.SignalJourneyAudienceConstraints.audienceId,
)
.join(
models.SingalJourneySources.source,
models.SingalJourneySources.sourceId
== models.SignalJourneyAudienceConstraints.sourceId,
)
.join(
models.SignalJourneyConstraintType.constraintType,
models.SignalJourneyConstraintType.constraintTypeId
== models.SignalJourneyAudienceConstraints.constraintTypeId,
)
.all()
)
我是否正确地认为 db.query() 是我的选择子句,然后我执行诸如连接、过滤等操作?
任何帮助将不胜感激
I think i am pretty close here, but maybe i need some pointers. I am trying to run the SQL query below in SQL Alchemy.
Endpoints for populating the UI with data. These need to consist of some joins.
select
constraintid,
sja.segment,
sjs.source,
sjct.constrainttype,
constraintvalue,
targeting,
frequency,
period
from signaljourneyaudienceconstraints
JOIN signaljourneyaudiences sja ON sja.audienceid = signaljourneyaudienceconstraints.audienceid;
JOIN signaljourneysources sjs ON sjs.sourceid = signaljourneyaudienceconstraints.sourceid
JOIN signaljourneyconstrainttype sjct ON sjct.constrainttypeid = signaljourneyaudienceconstraints.constrainttypeid
I have read some docs, I think I am on the right lines but I keep getting errors like
sqlalchemy.exc.ArgumentError: Join target SignalJourneyAudiences.segment does not refer to a mapped entity
Which I get because SignalJourneyAudiences.segment
is not a key, I was trying to replicate the SQL query which I know works.
@app.get("/get-main-query-data", status_code=status.HTTP_200_OK)
def get_main_query_data():
"""Returns data for the main query."""
return (
db.query(
models.SignalJourneyAudienceConstraints.constraintId,
models.SignalJourneyAudiences.segment,
models.SingalJourneySources.source,
models.SignalJourneyConstraintType.constraintType,
models.SignalJourneyAudienceConstraints.constraintValue,
models.SignalJourneyAudienceConstraints.targeting,
models.SignalJourneyAudienceConstraints.frequency,
models.SignalJourneyAudienceConstraints.period,
)
.join(
models.SignalJourneyAudiences.segment,
models.SignalJourneyAudiences.audienceId
== models.SignalJourneyAudienceConstraints.audienceId,
)
.join(
models.SingalJourneySources.source,
models.SingalJourneySources.sourceId
== models.SignalJourneyAudienceConstraints.sourceId,
)
.join(
models.SignalJourneyConstraintType.constraintType,
models.SignalJourneyConstraintType.constraintTypeId
== models.SignalJourneyAudienceConstraints.constraintTypeId,
)
.all()
)
Am I right in thinking the db.query()
is my select claus and then I do things like joins, filters etc?
any help would be greatly appreciated
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我已经解决了这个问题,我知道我走在正确的轨道上。
我正在使用
而不是
I have resolved the issue, i knew I was on the right track.
I was using
instead of