2012-02-21 78 views
0

我发现answer对我的问题有影响,但格式错误。从表A到表B加入两列

使用SQL炼金术我想加入从表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", 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 

而是因为我需要映射两个相同的表列,是越来越糊涂了。

有没有人知道实现这一目标的正确方法?

回答

1

我打算删除这个问题,但这不是重复的。答案是here(设置主要和次要连接)

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) 
     }) 
相关问题