可以在sqlalchemy中指定类而不指定__tablename__吗?

时间:2012-11-27 22:34:21

标签: python sqlalchemy

我正在尝试创建一个基类,为方便起见,指定了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中是否可行?

1 个答案:

答案 0 :(得分:7)

请参阅__abstract__

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