2011-12-26 1 views
0

은 다음 표입니다 : |persons||images||types|입니다상속을 사용할 때 관계에 대한 특정 유형 가져 오기? 내가 지금까지 무엇을 가지고

|nodes| |types| |persons| |images| 

|nodes||types|있다.

지금까지 SQLAlchemy의 선언적 구문을 사용하여 데이터베이스 스키마를 표현하려고 했으므로 Python에서 데이터베이스를 쉽게 쿼리 할 수 ​​있습니다. 그러나 문제는 내가 특정 Person (또는 무엇이든) 클래스 대신 일반 Type 클래스를 반환하는 node.types를 쿼리 할 때입니다.

class Node(Base): 
    __tablename__ = 'nodes' 
    __table_args__ = {'autoload': True} 

    types = relationship("Type") 

class Type(Base): 
    __tablename__ = 'types' 
    __table_args__ = {'autoload': True} 

    node_id = Column("node_id", Integer, ForeignKey('nodes.id')) 

class Person(Type): 
    __tablename__ = "persons" 
    __table_args__ = {'autoload': True} 
    __mapper_args__ = {'polymorphic_identity': '/people/person'} 

    id = Column("id", Integer, ForeignKey('types.id'), primary_key=True) 

예 :

session = Session() 
print session.query(Node).one().types[0] 
... <__main__.Type object at 0x9a4decc> 

이 어떻게 쉽게 대신 제네릭 형식의 특정 유형을 얻을 수 있습니다 여기에

는 지금까지이 무엇입니까?

답변

1

Nevermind. Type 클래스에 polymorphic_on을 추가하는 것을 잊었습니다. 즉 :

class Type(Base): 
    __tablename__ = 'types' 
    __table_args__ = {'autoload': True} 

    type = Column("type", String) 
    node_id = Column("node_id", Integer, ForeignKey('nodes.id')) 

    __mapper_args__ = {'polymorphic_on': type} 
관련 문제