The practice of mapping to arbitrary SELECT statements, especially complex ones as above, is almost never needed; it necessarily tends to produce complex queries which are often less efficient than that which would be produced by direct query construction. The practice is to some degree based on the very early history of SQLAlchemy where the mapper() construct was meant to represent the primary querying interface; in modern usage, the Query object can be used to construct virtually any SELECT statement, including complex composites, and should be favored over the “map-to-selectable” approach.
如果您只想从连接中拉入一个额外的列,那么我就不想使用任意的select映射。作为documentation points out:
您只需在应用程序中选择额外的列:
或者可以在
Table1Model
和association property上注册一个关系,该关系总是拉入额外的列:当您在
Table1Model
实例上与关联行交互时,association属性管理关联行的Table2Model.description
列。你知道吗这就是说,如果您必须使用} mapper argument 从联接中排除额外的重复列:
join()
查询作为基础,那么您可以使用^{然后,新模型使用联接中的所有列来创建具有相同名称的属性,但“exclude\u properties”中列出的属性除外。你知道吗
或者,您可以继续在模型中使用重复的列名,只需给它们一个新名称:
可以通过这种方式重命名联接中的任何列,此时它们将不再与另一个表中具有相同基名称的其他列冲突。你知道吗
相关问题 更多 >
编程相关推荐