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

                    THEN -1
                    ELSE "cte"."depth"
                END AS "depth"
            FROM "locations_sqllocation", "cte"
            WHERE
                "locations_sqllocation"."is_archived" = FALSE
                AND "locations_sqllocation"."id" = "cte"."parent_id"
        )

        SELECT DISTINCT "cte"."loc_id", "cte"."depth" FROM "cte"

    ), fixture_ids AS (
        SELECT
            "locations_sqllocation"."id",
            "locations_sqllocation"."parent_id",
            Array ["locations_sqllocation"."id"] AS "path",
            0 AS "depth"
        FROM "locations_sqllocation"
        WHERE
            "locations_sqllocation"."is_archived" = FALSE
            AND "locations_sqllocation"."domain" = domain_name
            AND "locations_sqllocation"."parent_id" IS NULL
            AND EXISTS (
                SELECT 1
                FROM "expand_from" U0

            UNION ALL

            SELECT
                loc."parent_id",
                CASE
                    WHEN (
                        loc_type."expand_from_root" = FALSE
                        AND loc_type."expand_from" IS NOT NULL
                        AND NOT (
                            loc_type."expand_from" = loc."location_type_id"
                            AND loc_type."expand_from" IS NOT NULL
                        )
                        AND NOT EXISTS (
                            SELECT 1
                            FROM "locations_locationtype" U0
                            INNER JOIN "locations_locationtype_include_only" U1 ON U0."id" = U1."to_locationtype_id"
                            WHERE U1."from_locationtype_id" = loc."location_type_id"
                        )
                    )
                    THEN loc_type."expand_from"
                    ELSE NULL
                END AS "expand_from_type",
                CASE
                    WHEN loc_type."expand_from_root" = TRUE THEN NULL
                    ELSE loc."id"
                END AS "loc_id",
                CASE
                    WHEN loc_type."expand_to_id" IS NOT NULL THEN (
                        SELECT U0."expand_to_depth"
                        FROM "expand_to" U0
                        WHERE U0."expand_to_type" = loc_type."expand_to_id"
                    )
                    WHEN EXISTS (
                        SELECT 1
                        FROM "locations_locationtype" U0
                        INNER JOIN "locations_locationtype_include_only" U1
                            ON U0."id" = U1."to_locationtype_id"
                        WHERE U1."from_locationtype_id" = loc."location_type_id"
                    )
                    THEN -3
                    ELSE -2
                END AS "depth"
            FROM "locations_sqllocation" loc
            INNER JOIN "locations_locationtype" loc_type ON loc."location_type_id" = loc_type."id"
            WHERE
                loc."is_archived" = FALSE
                AND loc."domain" = domain_name
                AND loc."id" = ANY(user_location_ids_array)

            UNION ALL

            SELECT
                loc."parent_id",
                CASE
                    WHEN (
                        "cte"."expand_from_type" IS NOT NULL
                        AND "cte"."expand_from_type" <> loc."location_type_id"
                    )
                    THEN "cte"."expand_from_type"
                    ELSE NULL
                END AS "expand_from_type",
                CASE
                    WHEN "cte"."loc_id" IS NULL THEN NULL
                    ELSE loc."id"
                END AS "loc_id",
                CASE
                    WHEN (
