Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 210
disappearedng Avatar asked Dec 20 '22 12:12

disappearedng


1 Answers

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)
like image 198
van Avatar answered Dec 28 '22 08:12

van