2009-10-05 1 views
0

다음 관계를 설명 할 수있는 SQLAlchemy/Elixer 모델을 정의하려고합니다. 나는 POC 테이블에 여러 개의 외래 키가있는 SSP 테이블이 있습니다. SSP 개체 내에서 ManyToOne 관계를 올바르게 정의했습니다 (정확하게 SSP.get(1).action.first_name 수 있습니다). 또한이 관계의 다른 측면 인 POC.get(1).csa을 수행하고이 POC가 idPOCCSA로 정의 된 SSP 개체의 목록을 반환 할 수 있습니다.Python SQLAlchemy/Elixer 질문

이것은 다형성 연관에 가장 좋음을 알고 있지만 실제로는 DB 스키마를 변경할 수 없습니다 (type 연관을위한 새로운 poc2ssp 테이블 생성).

class POC(Entity): 
    using_options(tablename = 'poc', autoload = True) 

    # These two line visually display my "issue": 
    # csa = OneToMany('SSP') 
    # action = OneToMany('SSP') 


class SSP(Entity): 
    ''' 
    Many to One Relationships: 
    - csa: ssp.idPOCCSA = poc.id 
    - action: ssp.idPOCAction = poc.id 
    - super: ssp.idSuper = poc.id 
    ''' 
    using_options(tablename = 'spp', autoload = True) 

    csa = ManyToOne('POC', colname = 'idPOCCSA') 
    action = ManyToOne('POC', colname = 'idPOCAction') 
    super = ManyToOne('POC', colname = 'idPOCSuper') 

어떤 아이디어가 있습니까? Elixer FAQ에는 primaryjoin 및 foreign_keys 매개 변수를 사용하는 좋은 예가 있지만 설명서에서 찾을 수 없습니다. OneToMany()가 ManyToOne()처럼 colname 매개 변수를 지원하기를 바랬습니다. 조금 덜 장황한 것.

답변

1

은 다음을 시도해보십시오

class POC(Entity): 
    # ... 
    #declare the one-to-many relationships 
    csas = OneToMany('SSP') 
    actions = OneToMany('SSP') 
    # ... 

class SSP(Entity): 
    # ... 
    #Tell Elixir how to disambiguate POC/SSP relationships by specifying 
    #the inverse explicitly. 
    csa = ManyToOne('POC', colname = 'idPOCCSA', inverse='csas') 
    action = ManyToOne('POC', colname = 'idPOCAction', inverse='actions') 
    # ...  
+0

작품 완벽하게, 내가 OneToOne() 관계로 역을 이용되었으며, OneToMany에 적용 생각하지 않았다(). –