From 0dc5e58b55472cab033c4e53924226f62f612f57 Mon Sep 17 00:00:00 2001 From: AI Assistant Date: Tue, 24 Mar 2026 16:16:06 +0200 Subject: [PATCH] fix(geoportal): use subquery instead of JOIN for gis_cladiri_status view LEFT JOIN caused duplicate rows and column conflicts. Replaced with a correlated subquery (LIMIT 1) to safely look up BUILD_LEGAL from the parent parcel's enrichment. Co-Authored-By: Claude Opus 4.6 (1M context) --- .../geoportal/setup-enrichment-views/route.ts | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/src/app/api/geoportal/setup-enrichment-views/route.ts b/src/app/api/geoportal/setup-enrichment-views/route.ts index 5c4ebd5..f18b946 100644 --- a/src/app/api/geoportal/setup-enrichment-views/route.ts +++ b/src/app/api/geoportal/setup-enrichment-views/route.ts @@ -47,14 +47,18 @@ const VIEWS = [ f."cadastralRef" AS cadastral_ref, f."areaValue" AS area_value, f."isActive" AS is_active, - COALESCE((p.enrichment->>'BUILD_LEGAL')::int, -1) AS build_legal, + COALESCE( + (SELECT (p.enrichment->>'BUILD_LEGAL')::int + FROM "GisFeature" p + WHERE p.siruta = f.siruta + AND p."cadastralRef" = f."cadastralRef" + AND (p."layerId" LIKE 'TERENURI%' OR p."layerId" LIKE 'CADGEN_LAND%') + AND p.enrichment IS NOT NULL + LIMIT 1), + -1 + ) AS build_legal, f.geom FROM "GisFeature" f - LEFT JOIN "GisFeature" p - ON p.siruta = f.siruta - AND p."cadastralRef" = f."cadastralRef" - AND (p."layerId" LIKE 'TERENURI%' OR p."layerId" LIKE 'CADGEN_LAND%') - AND p.enrichment IS NOT NULL WHERE f.geom IS NOT NULL AND (f."layerId" LIKE 'CLADIRI%' OR f."layerId" LIKE 'CADGEN_BUILDING%')`, },