如何在select查询中设置表别名

2024-07-04 13:38:53 发布

您现在位置:Python中文网/ 问答频道 /正文

默认情况下,peewee为表分配t1t2等名称 我试图在peewee中构建一些复杂的查询,所以我想对表名进行别名。在

让我们假设一个简单的例子:我们有一个表<MyTable>,我怎么能得到这样的结果

select foo.id from MyTable as foo;

我尝试的是设置alias,但看起来它是一个类方法,但它不起作用:

^{pr2}$

我找到了AliasMap类,它被设置为QueryCompileralias_map_class类变量的默认值。另外,在QueryCompiler中有alias方法,但它是为了other目的而设计的:

The alias() method is used to create self-joins.


Tags: 方法from名称idfooasmytable情况
2条回答

好吧,我得再深入研究一下documentation

class MyTable(Model):
    class Meta:
        table_alias = 'my_alias'
car_model = Cars.as_entity().alias('car_type_query')
car_type_query = Cars.select(car_model.c.car_type_id)\
                .from_(car_model)\
                .where(car_model.c.id == car)

相关问题 更多 >

    热门问题