繁体   English   中英

如何使用@>运算符构建具有过滤器的sqlalchemy查询?

[英]How to build a sqlalchemy query that has a filter with the @> operator?

我有这个烧瓶/ sqlalchemy查询,其工作原理如下:

paged_data = House.query\
    .with_entities(
        House.name,
        db.func.earth_distance(
            db.func.ll_to_earth(current_latitude, current_longitude),
            House.earth_location
        ).label('house_distance_from_current_location')
    )\
    .filter(db.text('earth_box(ll_to_earth(:latitude, :longitude), :radius)'
                    ' @> ll_to_earth(houses.latitude, houses.longitude)'
                   )
    )\
    .params(latitude=latitude, longitude=longitude, radius=radius)\
    .all()

但是我更喜欢清理它,使其看起来更像下面的代码...显然,它不会那样工作...但我不知道如何编写...尤其是@>运营商是不是有效的Python一个方式>运营商。

paged_data = House.query\
    .with_entities(
        House.name,
        db.func.earth_distance(
            db.func.ll_to_earth(current_latitude, current_longitude),
            House.earth_location
        ).label('house_distance_from_current_location')
    )\
    .filter(
        db.func.earth_box(db.func.ll_to_earth(current_latitude, current_longitude), radius) @> House.earth_location
    )\
    .params(latitude=latitude, longitude=longitude, radius=radius)\
    .all()

有没有办法做到这一点,或者我只需要使用text()

供参考的是House模型(它使用了可以正常工作的create_materialized_view()函数):

class House(db.Model):
    __table__ = create_materialized_view(
        'houses',
        db.select([
            House.id.label('id'),
            House.name.label('name'),
            House.latitude.label('latitude'),
            House.longitude.label('longitude'),
            db.func.ll_to_earth(House.latitude, House.longitude).label('earth_location'),
        ]).where(
            db.and_(
                House.reviewed == True,
                House.deleted == False
            )
        )
    )

您需要操作方法

.filter(
    db.func.earth_box(db.func.ll_to_earth(current_latitude, current_longitude), radius)
    .op('@>')(House.earth_location)
)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM