2017-04-17 14 views
1

我有SQLAlchemy的沒有經驗,我有以下代碼:使用sqlalchemy時,__table_args__的用途是什麼?

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__的目的,什麼以前的一段代碼在做什麼。

回答

2

該屬性適用於位置以及通常發送到Table構造函數的關鍵字參數。

在施工過程中申報模式類 - ForecastBedSetting你的情況 - 隨附Basecreates an instance of Table元類。 __table_args__屬性允許將額外的參數傳遞給該Table。所創建的表是通過訪問

ForecastBedSetting.__table__ 

代碼defines an index inline,傳遞Index實例的創建Table。該索引使用字符串名稱來標識列,所以不傳遞給表SQLAlchemy無法知道它屬於哪個表。

+0

非常感謝你 – lmiguelvargasf