Usage of “aliased” in SQLAlchemy ORM

后端 未结 2 956
无人共我
无人共我 2021-01-01 17:04

From the SQLAlchemy ORM Tutorial:

You can control the names using the label() construct for scalar attributes and aliased for class constructs:

2条回答
  •  别那么骄傲
    2021-01-01 17:36

    As @jd. said,mostly when using the same table more than once in a query. Example:

        dict_code_type, dict_code_status = aliased(DictCode), aliased(DictCode)
        query = Device.query \
          .join(dict_code_type, dict_code_type.codeValue == Device.deviceType) \
          .join(dict_code_status, dict_code_status.codeValue == Device.status) \
          .with_entities(Device.id, Device.deviceName, Device.status,
                       Device.deviceID, Device.deviceUsername, Device.token,
                       dict_code_type.codeLabel.label('deviceTypeLabel'),
                       dict_code_status.codeLabel.label('statusLabel'),  Device.createAt, Device.authType) \
        .filter(and_(dict_code_type.code == 'deviceType', dict_code_status.code == 'status'))
    

提交回复
热议问题