4

我正在尝试创建一个基类,其中为方便起见指定了 id 和 url。

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 中是否可行?

4

1 回答 1

11

请参阅抽象具体类

class CrawlableEntity(Base):
  """CrawlableEntity class for all entities crawled online.  """
  __abstract__ = True
  id = Column(Integer, primary_key=True)
  url = Column(String, nullable=False)
于 2012-11-29T07:18:28.277 回答