-
Notifications
You must be signed in to change notification settings - Fork 11k
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
[5.3.26] BelongsToMany withCount selects incorrect id. #16645
Comments
You are correct. The generated query looks like this: SELECT *,
(SELECT count(*)
FROM `permissions`
INNER JOIN `permission_role` ON `permissions`.`id` = `permission_role`.`permission_id`
WHERE `permission_role`.`role_id` = `roles`.`id`) AS `permissions_count`
FROM `roles`
INNER JOIN `role_user` ON `roles`.`id` = `role_user`.`role_id`
WHERE `role_user`.`user_id` = 9 And it's result is:
First 5 columns are from the As you can see, the
They are. Try this: $user = \App\User::first();
$pivot = $user->roles()
->withCount('permissions')
->get()
->first()
->pivot;
dd($pivot->toArray()); For me, its output is:
Therefore you should probably use this value to get the role id. Nevertheless, this is a bug in my opinion. |
@KKSzymanowski do you think you can open a PR with a fix? |
I'm not really sure what to do with this. I would need to take a look how it's done in other parts of the Eloquent engine, because I'm not that familiar with relationship logic. Give me a day or two, maybe you'll come up with something in the meantime. If so, please share any ideas. |
@cabalopolis have you tried it like this as well:
|
I haven't tested but it but I wouldn't expect it to work because these are the default values for belongsToMany relationship. |
@cabalopolis @KKSzymanowski What you guys have described here, i haven't been able to replicate. My view is that you guys haven't done your due diligence in this regard. I have exactly the table structure as well described by @cabalopolis. This just doesn't happen. I am attaching screenshots of what i did through |
@srmklive I'm not 100% sure you have understood the issue / performed that steps correctly. I have set up a repository with the issue outlined, feel free to clone it and observe the issue: https://github.com/cabalopolis/with-count-bug. |
@cabalopolis I have done it correctly. You in my view are not doing it right 😆. I have cloned your repo, performed the exact steps you mentioned and couldn't replicate the issue you are supposedly facing. All i did was setup model factory config for |
Furthermore if such a issue was happening, i am sure the issue section would have flooded with this, and fix would have been issued instantly. |
@srmklive you cannot see the issue as it is being masked by the fact that the pivot table entry has an ID of 1 and the roles table as and ID of 1. If the entry in the pivot table were to have an ID of 2 then you would see the issue. |
@cabalopolis here you go then 😉 |
@srmklive Run the migrations + seeds in the repo and perform the same as I have done in tinker. |
I can reproduce it too, with your repo... 😸 Reproduced with sqlite... |
@srmklive You didn't see the bug because you created a role with an id of 1 and the pivot table had the id of 1. Then you created a second role(id = 2) and when you attached to the user the pivot table once again had the same id. |
@KKSzymanowski It doesn't matter which id i attach it in the pivot table, i always got the data with correct id. What @cabalopolis is referring to an issue with a single entity i guess. |
@stmklive the issue is not limited to any single entry. @KKSzymanowski is correct, the behaviour of php and the generated query is what is causing the bug. It appears that the pivot table and the roles table are being merged, which is why the role_id and the user_id is appearing twice (once in the array and once in the pivot key of the array). |
@cabalopolis The issue which @KKSzymanowski alluded to was that eloquent shouldn't select the pivot table id column, I agree with that. The reason I went back & forth with you was the issue with data representation you listed, which I couldn't replicate. |
Description:
With a belongsToMany => belongsToMany, using the Builder::withCount() causes the incorrect
id
to be selected, it appears theid
from the pivot table is being selected rather than the table itself.Here are the models:
users
)users
roles
)(role_user
)(permission_role
)roles
role_user
permission_role
permissions
)Steps To Reproduce:
id
is 4 (from therole_user
table), when it should be 2 (from theroles
table).Additionally: the
role_id
and theuser_id
should be in apivot
key.The text was updated successfully, but these errors were encountered: