Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Don't add BelongsToMany relation column to select #2721

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

affaron
Copy link

@affaron affaron commented Dec 29, 2021

It seems like a part of "duplicate column" problems with sorting/filtering by BelongsToMany relation column are coming from this.

When you have same column names in your main and relation tables adding a ->select() is not helping because the relation column is still added in joinEagerLoadedColumn(). And depending on the situation either your main model column will be populated with same name relation column or you're getting an error in your aggregate queries. Example:

Exception Message:

SQLSTATE[42S21]: Column already exists: 1060 Duplicate column name 'name' (SQL: select count(*) as aggregate from (select `crm_companies`.`id`, `crm_companies`.`name`, `dic_company_types`.`name` from `crm_companies` left join `crm_company_types` on `crm_company_types`.`company_id` = `crm_companies`.`id` left join `dic_company_types` on `crm_company_types`.`type_id` = `dic_company_types`.`id` where LOWER(`dic_company_types`.`name`) LIKE %clearing agent% group by `crm_companies`.`name`) count_row_table)

(crm_companies.name is the main model column, dic_company_types.name - automatically added relation column and hence the conflict)

So I look into the code and found this place. $this->performJoin($table, $foreign, $other); looks unnecessary since it's performed anyway after the switch block and addSelect is adding a relation column that doesn't seem to be required and causes duplicates. Please correct me if I'm wrong.

Thanks

@yajra
Copy link
Owner

yajra commented Feb 1, 2022

Per @dragonfly4 feedback, PR doesn't fixed the issue.

I tested that but it's not fixing the issue above. Same error.

@yajra yajra changed the title dont add BelongsToMany relation column to select Dont add BelongsToMany relation column to select Feb 1, 2022
@yajra yajra changed the title Dont add BelongsToMany relation column to select Don't add BelongsToMany relation column to select Feb 1, 2022
@yajra yajra changed the base branch from 9.0 to master July 1, 2022 09:12
src/DataTables.php Outdated Show resolved Hide resolved
src/DataTables.php Outdated Show resolved Hide resolved
@affaron affaron force-pushed the fix-duplicate-columns-in-relation-sorting branch from a9ce531 to bd9f567 Compare September 23, 2022 09:48
@affaron
Copy link
Author

affaron commented Sep 23, 2022

Hey, sorry, didn't have time to get back to this. I reverted those changes from a merge I did into this branch back then. Didn't realise that it'd affect this request.

Anyway, it still seems that adding that addSelect (even the recent one in HasOneThrough case) causes some column names duplicating when you sort/search (if you already have select attached before). Maybe it's best to remove them and suggest users to use selects (it was already suggested as I recall).

I'll try to look into this issue again later.

@turdakhov
Copy link

turdakhov commented Jan 31, 2023

I have duplicates without using select.
I solved it this way:
return $this->belongsToMany(Characteristic::class)->distinct()->with('values');

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

Successfully merging this pull request may close these issues.

3 participants