Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

safe parameter bindings in sqlalchemy filter

I need to pass a partial raw sql query into sqlalchemy filter, like

s.query(account).filter("coordinate <@> point(%s,%s) < %s"%(lat,long,distance))

Yes, I'm trying to use earthdistance function in postgresql.

Of course, I could use PostGis and GeoAlchemy2, but I want to know the general solution to this kind of problems.

I know sqlalchemy can safely pass raw sql query .

result = db.engine.execute("select * coordinate <@> point(:lat,:long) < :distance",**params)

Is there any similar function that can be used to bind parameter of partial(?) sql query? I guess someone who implements custom sql function like func.ll_to_earth have used the function.

like image 930
12412316 Avatar asked Oct 23 '25 08:10

12412316


1 Answers

There is .params() on query. Try this:

query = s.query(account).filter(
    "coordinate <@> point(:lat, :long_) < :dist").params(
        lat=lat, long_=long_, dist=distance)

And there is the documentation on it.

Note: I renamed your long param, because there is alread a __builtin__ named long (long int) in python, it's good practice to not overwrite already used words for obvious reasons.

like image 81
knitti Avatar answered Oct 25 '25 21:10

knitti



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!