                             OR U0."depth" = -2
                             OR U0."depth" >= 0
                         )
-                        AND U0."loc_id" = "locations_sqllocation"."id"
+                        AND U0."loc_id" = loc."id"
                     ) OR (
                         (
                             U0."depth" = -2
                             OR U0."depth" >= 0
                         ) AND (
                             U0."loc_id" IS NULL
-                            OR U0."loc_id" = "locations_sqllocation"."id"
-                        )
-                    ) OR (
-                        (
-                            U0."loc_id" = "locations_sqllocation"."id"
-                            OR U0."loc_id" = "locations_sqllocation"."id"
+                            OR U0."loc_id" = loc."id"
+                        )
+                    ) OR (
+                        (
+                            U0."loc_id" = loc."id"
+                            OR U0."loc_id" = loc."id"
                         )
                         AND U0."depth" = -3
-                        AND "locations_sqllocation"."location_type_id" IN (
+                        AND loc."location_type_id" IN (
                             SELECT to_locationtype_id
                             FROM locations_locationtype_include_only
-                            WHERE from_locationtype_id IN (
-                                SELECT "locations_sqllocation"."location_type_id"
-                                FROM "locations_sqllocation"
-                                WHERE "locations_sqllocation"."id" = ANY(user_location_ids_array)
-                            )
+                            INNER JOIN "locations_sqllocation" x
+                                ON x."location_type_id" = from_locationtype_id
+                            WHERE x."id" = ANY(user_location_ids_array)
                         )
                     )
             )

                            OR U0."depth" = -2
                            OR U0."depth" >= "fixture_ids"."depth" + 1
                        )
                        AND U0."loc_id" = "locations_sqllocation"."id"
                    ) OR (
                        (
                            U0."depth" = -2

        UNION ALL

        SELECT
            loc."id",
            loc."parent_id",
            array_append("fixture_ids"."path", loc."id") AS "path",
            "fixture_ids"."depth" + 1 AS "depth"
        FROM "locations_sqllocation" loc
        INNER JOIN "fixture_ids" ON loc."parent_id" = "fixture_ids"."id"
        WHERE
            loc."is_archived" = FALSE
            AND loc."domain" = domain_name
            AND EXISTS (
                SELECT 1
                FROM "expand_from" U0
