2012-10-05 30 views
0

我的表是這樣的:映射針對可選擇和獲得正確的主鍵中的SQLAlchemy

from sqlalchemy import Column, Integer, select 
from sqlalchemy.ext.declarative import declarative_base 
Base = declarative_base() 

class Blah(Base): 
    __tablename__ = 'Blah' 
    container_id = Column(Integer, primary_key=True) 
    blah_id = Column(Integer, primary_key=True) 
    gprop = Column(Integer, index=True, nullable=False) 

class GProp(Base): 
    __table__ = select([ 
     Blah.container_id, Blah.gprop 
    ]).group_by(
     Blah.container_id, Blah.gprop 
    ).alias() 

因爲它是GProp.__table__.primary_key拾起container_id列,但我不能找到一種方法,使gprop主鍵的列部分。這打破了我的查詢,SQLAlchemy將只爲每個容器加載一個GProp

失敗修復至今:添加Column屬性是被禁止的,因爲它重新定義現有列,增加一個PrimaryKeyConstraint__table_args__沒有效果,和猴子修補__table__.primary_key得到由剛剛錯斷言抓獲。

回答

2

OK,加入

__mapper_args__ = dict(primary_key=[__table__.c.container_id, __table__.c.gprop]) 

做工作,即使它不會反映在__table__.primary_key