2012-11-27 38 views
3

我想創建一個基類,其中id和url是爲了方便而指定的。可以在sqlalchemy中指定沒有指定__tablename__的類嗎?

Base = declarative_base() 
Base.query = session.query_property() 


class CrawlableEntity(Base): 
    """CrawlableEntity class for all entities crawled online. 
    """ 
    id = Column(Integer, primary_key=True) 
    url = Column(String, nullable=False) 


class Model(CrawlableEntity): 
    __tablename__ = 'models' 
    name = Column(String)  # Bobby Raffin 
    looks = relationship('Look', backref='model') 

我收到以下錯誤

sqlalchemy.exc.InvalidRequestError: Class <class 'CrawlableEntity'> does not have a __table__ or __tablename__ specified and does not inherit from an existing table-mapped class. 

是這種模式可能在SQLAlchemy的?

回答

6

__abstract__

class CrawlableEntity(Base): 
    """CrawlableEntity class for all entities crawled online. """ 
    __abstract__ = True 
    id = Column(Integer, primary_key=True) 
    url = Column(String, nullable=False) 
+2

*慢拍*你怎麼連這個都知道? – disappearedng