Repository: typeorm/typeorm
File: src/query-builder/transformer/RawSqlResultsToEntityTransformer.ts (TypeScript)

Code before:
    protected group(rawResults: any[], alias: Alias): Map<string, any[]> {
        const map = new Map()
        const keys: string[] = []
        if (alias.metadata.tableType === "view") {
            keys.push(
                ...alias.metadata.columns.map((column) =>
                    this.buildAlias(alias.name, column.databaseName),
                ),
            )
        } else {
            keys.push(
                ...alias.metadata.primaryColumns.map((column) =>
                    this.buildAlias(alias.name, column.databaseName),
                ),
            )
        }

        // Check if primary key columns are actually selected in the raw results
        const hasValidKeys =
            keys.length > 0 &&
            rawResults.length > 0 &&
            keys.some(
                (key) =>
                    rawResults[0].hasOwnProperty(key) &&
                    rawResults[0][key] !== undefined,
            )

        for (const rawResult of rawResults) {
            let id: string

            if (hasValidKeys) {
                // Use primary key based grouping when available
                id = keys
                    .map((key) => {
                        const keyValue = rawResult[key]

                        if (Buffer.isBuffer(keyValue)) {
                            return keyValue.toString("hex")
                        }

                        if (ObjectUtils.isObject(keyValue)) {
                            return JSON.stringify(keyValue)
                        }

                        return keyValue
                    })
                    .join("_")
            } else {
                // Fallback: use row index when primary keys are not available
                // This ensures each row gets its own group for proper entity mapping
                const rowIndex = rawResults.indexOf(rawResult)

Review comment (suggestion):
```suggestion
        const hasValidKeys = keys.some(key => key in (rawResults[0] ?? {}))
```

Code after:
    protected group(rawResults: any[], alias: Alias): Map<string, any[]> {
        const map = new Map()
        const keys: string[] = []
        if (alias.metadata.tableType === "view") {
            keys.push(
                ...alias.metadata.columns.map((column) =>
                    this.buildAlias(alias.name, column.databaseName),
                ),
            )
        } else {
            keys.push(
                ...alias.metadata.primaryColumns.map((column) =>
                    this.buildAlias(alias.name, column.databaseName),
                ),
            )
        }

        // Check if primary key columns are actually selected in the raw results
        const primaryKeysSelected = keys.some(
            (key) => key in (rawResults[0] ?? {}),
        )

        for (const rawResult of rawResults) {
            let id: string

            if (primaryKeysSelected) {
                // Use primary key based grouping when available
                id = keys
                    .map((key) => {
                        const keyValue = rawResult[key]

                        if (Buffer.isBuffer(keyValue)) {
                            return keyValue.toString("hex")
                        }

                        if (ObjectUtils.isObject(keyValue)) {
                            return JSON.stringify(keyValue)
                        }

                        return keyValue
                    })
                    .join("_")
            } else {
                // Fallback: use row index when primary keys are not available
                // This ensures each row gets its own group for proper entity mapping
                const rowIndex = rawResults.indexOf(rawResult)
                id = `row_${rowIndex}`
            }

            const items = map.get(id)
            if (!items) {