xjq233p_1
xjq233p_1

Reputation: 8060

Possible to specify class without specifying __tablename__ in sqlalchemy?

I am trying to create a base class where id and url are specified for convenience.

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')

I am getting the following error

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

Is this pattern possible in sqlalchemy?

Upvotes: 10

Views: 3984

Answers (1)

van
van

Reputation: 76962

See Abstract Concrete Classes:

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

Upvotes: 15

Related Questions