2012-02-21 150 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) 
     }) 
相關問題