Skip to content

Conversation

@Seb33300
Copy link
Contributor

When using complex queries with joins, I am often facing ambiguous column name errors when sorting a column of the base table sharing the same name as a column of a relation table field (eg: created_at)

Until now, the workaround I used was to rename my columns

// From
DateColumn::make('created_at')`
// To
DateColumn::make('created_at', 'table.created_at')

But it's probably better to fix the root cause by forcing the table name if the column is not a relation.

And it's consistent with $this->joinEagerLoadedColumn() which is returning columns name with alias.

@Seb33300 Seb33300 marked this pull request as draft April 22, 2025 03:30
@Seb33300 Seb33300 marked this pull request as ready for review April 22, 2025 03:55
@yajra
Copy link
Owner

yajra commented Apr 28, 2025

@Seb33300 the patch looks great. However, found a failing use case:

Given this query:

    public function query(User $model): QueryBuilder
    {
        return $model->newQuery()
            ->withCount('permissions');
    }

With column defined as:

            Column::computed('permissions_count')
                ->title('Permissions')
                ->orderable()

The expectation is the ordering should still work. Since the patch adds a prefix, the ordering now fails.

Exception Message:

Error Code    : 904
Error Message : ORA-00904: "USERS"."PERMISSIONS_COUNT": invalid identifier
Help: https://docs.oracle.com/error-help/db/ora-00904/
Position      : 289
Statement     : select t2.* from ( select rownum AS "rn", t1.* from (select "USERS".*, (select count(*) from "PERMISSIONS" inner join "PERMISSION_USER" on "PERMISSIONS"."ID" = "PERMISSION_USER"."PERMISSION_ID" where "USERS"."ID" = "PERMISSION_USER"."USER_ID") as "PERMISSIONS_COUNT" from "USERS" order by "USERS"."PERMISSIONS_COUNT" asc) t1 ) t2 where t2."rn" between 1 and 20
Bindings      : []
 (Connection: oracle, SQL: select t2.* from ( select rownum AS "rn", t1.* from (select "USERS".*, (select count(*) from "PERMISSIONS" inner join "PERMISSION_USER" on "PERMISSIONS"."ID" = "PERMISSION_USER"."PERMISSION_ID" where "USERS"."ID" = "PERMISSION_USER"."USER_ID") as "PERMISSIONS_COUNT" from "USERS" order by "USERS"."PERMISSIONS_COUNT" asc) t1 ) t2 where t2."rn" between 1 and 20)

@Seb33300
Copy link
Contributor Author

@yajra I just pushed a fix for this

@sonarqubecloud
Copy link

sonarqubecloud bot commented May 2, 2025

@yajra
Copy link
Owner

yajra commented May 5, 2025

Wow, this patch is awesome @Seb33300 💯! This will solve a lot of previous issues.

Tested on several unique use cases that I can think of and it seems to work well.

@yajra yajra merged commit d1fad2e into yajra:master May 5, 2025
6 of 7 checks passed
@yajra
Copy link
Owner

yajra commented May 5, 2025

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants