gpt4 book ai didi

python - sqlalchemy 中 __table_args__ 的目的是什么?

转载 作者:行者123 更新时间:2023-12-01 13:35:44 24 4
gpt4 key购买 nike

我没有使用 sqlalchemy 的经​​验,我有以下代码:

class ForecastBedSetting(Base):
__tablename__ = 'forecast_bed_settings'
__table_args__ = (
Index('idx_forecast_bed_settings_forecast_id_property_id_beds',
'forecast_id', 'property_id',
'beds'),
)

forecast_id = Column(ForeignKey(u'forecasts.id'), nullable=False)
property_id = Column(ForeignKey(u'properties.id'), nullable=False, index=True)
# more definition of columns

虽然我查了 this ,我不明白 __table_args__ 的目的是什么,所以我不知道这条线在做什么:
__table_args__ = (
Index('idx_forecast_bed_settings_forecast_id_property_id_beds',
'forecast_id', 'property_id',
'beds'),
)

有人能解释一下 __table_args__的目的是什么吗? ,以及前一段代码在做什么。

最佳答案

This attribute accommodates both positional as well as keyword arguments that are normally sent to the Table constructor.



在声明性模型类的构建过程中 – ForecastBedSetting在您的情况下 - Base 附带的元类 creates an instance of Table . __table_args__属性允许将额外的参数传递给 Table .创建的表可以通过
ForecastBedSetting.__table__

代码 defines an index inline ,通过 Index实例到创建的 Table .索引使用字符串名称来标识列,因此如果没有传递给表,SQLAlchemy 无法知道它属于哪个表。

关于python - sqlalchemy 中 __table_args__ 的目的是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43455126/

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