Dictionary 将表A中的两列连接到表B
我找到了我问题的答案,但格式不对 我想使用SQL Alchemy将表A中的列连接到表B中的一列 表A包含两列位置代码。我可以通过连接到表B来检索位置名,但是如何做到这一点呢 到目前为止,我有:Dictionary 将表A中的两列连接到表B,dictionary,join,model,sqlalchemy,Dictionary,Join,Model,Sqlalchemy,我找到了我问题的答案,但格式不对 我想使用SQL Alchemy将表A中的列连接到表B中的一列 表A包含两列位置代码。我可以通过连接到表B来检索位置名,但是如何做到这一点呢 到目前为止,我有: locationreq = sa.Table("INMPTL_LOCATION_REQUEST", meta.metadata, sa.Column("request_id", sa.types.String(), primary_key=True), sa.Column("status"
locationreq = sa.Table("INMPTL_LOCATION_REQUEST", meta.metadata,
sa.Column("request_id", sa.types.String(), primary_key=True),
sa.Column("status", sa.types.String(100)),
sa.Column("new_loc", sa.types.String(), sa.ForeignKey("INMPTL_LOCATIONS_TBL.inmptl_location_code")),
sa.Column("previous_loc", sa.types.String(), sa.ForeignKey("INMPTL_LOCATIONS_TBL.inmptl_location_code")),
autoload=True,
autoload_with=engine)
locationtable = sa.Table("INMPTL_LOCATIONS_TBL", meta.metadata,
sa.Column("INMPTL_LOCATION_CODE", sa.types.Integer(), primary_key=True),
autoload=True,
autoload_with=engine)
orm.mapper(Location, locationtable )
orm.mapper(LocationRequest, locationreq, extension= wf.WorkflowExtension(), properties = {'location':relation(Location)}
如果这些列中只有一列映射到第二个表,我可以调用如下内容:
model.LocationRequest.location.location_name
但是因为我将两列映射到同一个表,所以它变得很混乱
有人知道实现这一点的正确方法吗?我本来打算删除这个问题,但这不是重复的问题。答案是(设置主联接和次联接)
orm.mapper(LocationRequest, locationreq, extension= wf.WorkflowExtension(),
properties={
"new_location":relation(Location,
primaryjoin=locationtable.c.inmptl_location_code==locationreq.c.new_loc, lazy = False),
"previous_location":relation(Location,
primaryjoin=locationtable.c.inmptl_location_code==locationreq.c.previous_loc, lazy = False)
})