Merge pull request 'fix(tradein): estimate 500 — алиас distance_m в ORDER BY' (#388) from fix/tradein-estimate-orderby-alias into main
All checks were successful
Deploy Trade-In / changes (push) Successful in 4s
Deploy Trade-In / build-frontend (push) Has been skipped
Deploy Trade-In / build-backend (push) Successful in 39s
Deploy Trade-In / deploy (push) Successful in 15s

This commit is contained in:
lekss361 2026-05-21 17:40:24 +00:00
commit 137c8bbb08

View file

@ -343,7 +343,10 @@ def _fetch_analogs(
AND scraped_at > NOW() - (:fresh_days || ' days')::interval AND scraped_at > NOW() - (:fresh_days || ' days')::interval
AND price_rub > 0 AND price_rub > 0
ORDER BY ( ORDER BY (
distance_m / 1000.0 -- distance_m это SELECT-алиас. В ORDER BY-ВЫРАЖЕНИИ (не голым
-- термом) PostgreSQL трактует имя как входную колонку listings,
-- которой нет "column distance_m does not exist". Инлайним ST_Distance.
ST_Distance(geom::geography, ST_MakePoint(:lon, :lat)::geography) / 1000.0
+ CASE WHEN :target_year IS NOT NULL AND year_built IS NOT NULL + CASE WHEN :target_year IS NOT NULL AND year_built IS NOT NULL
THEN abs(year_built - :target_year) / 12.0 ELSE 0 END THEN abs(year_built - :target_year) / 12.0 ELSE 0 END
+ CASE WHEN :target_house_type IS NOT NULL AND house_type IS NOT NULL + CASE WHEN :target_house_type IS NOT NULL AND house_type IS NOT NULL