
            WITH branch_check AS (
                SELECT 
                    from_b.parent_branch_id AS from_parent,
                    to_b.parent_branch_id AS to_parent,
                    from_b.branch_id AS from_id,
                    to_b.branch_id AS to_id
                FROM branches from_b, branches to_b
                WHERE from_b.branch_id = {{record.from_branch_id}}
                  AND to_b.branch_id = {{record.to_branch_id}}
                  AND from_b.isDeleted = false
                  AND to_b.isDeleted = false
            )
            SELECT 
                CASE 
                    -- Same parent (siblings)
                    WHEN from_parent IS NOT NULL AND from_parent = to_parent THEN true
                    -- Parent to child
                    WHEN from_id = to_parent THEN true
                    -- Child to parent
                    WHEN from_parent = to_id THEN true
                    ELSE false
                END AS are_related
            FROM branch_check
        