I am trying to calculate the minimum distance between two points in (longitude / latitude) in a database.
In SQLite I can write (example):
SELECT POWER(latitude-40,2) + POWER(longitude-32,2) * 0.5
FROM example_table
(0.5 is a correction factor for better approximation)
When I try to do the same thing in SQLAlchemy:
db.query((models.example_table.latitude - 40) * (models.example_table.latitude - 40) + (models.example_table.longitude - 32) * (models.example_table.longitude - 32) * 0.5).first()
then the produced SQL is wrong as it uses '||' instead of '+':
INFO:sqlalchemy.engine.Engine:SELECT (example_table.latitude - ?) * (example_table.latitude - ?) || (example_table.longitude - ?) * (example_table.longitude - ?) * ? AS anon_1
FROM example_table
LIMIT ? OFFSET ?
INFO:sqlalchemy.engine.Engine:[generated in 0.00078s] (43, 43, 32, 32, 0.5, 1, 0)
(-56.749055346000006,)
and of course the result is also wrong.
How can I fix this?