gpt4 book ai didi

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

转载 作者:太空狗 更新时间:2023-10-30 02:21:52 26 4
gpt4 key购买 nike

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

最佳答案

参见 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)

关于python - 可以在 sqlalchemy 中指定类而不指定 __tablename__ 吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13594413/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com