-
-
Notifications
You must be signed in to change notification settings - Fork 437
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
Limit a queries select to the model's table #1214
Comments
If anyone else runs into this scenario I ended up working around it by using a sub-query and applying an alias to the joined table's ID: public function scopeByTeam(Builder $query, int $teamId): Builder
{
$subQuery = Campaign::query()->select('id as campaignId')->where('team_id', '=', $teamId);
$query->joinSub(
$subQuery,
'team_campaigns',
function (JoinClause $join) {
$join->on('donations.campaign_id', '=', 'team_campaigns.campaignId');
}
);
return $query;
} |
Thanks for the detailed report. Can you add a PR with a failing test case? |
I've been running into this issue quite a bit too, so placed together a failing test in PR #1253. In the instance of attempting to select users with ID 1 & 2, joining the team table (with ID 1) will change all users in the query to |
Thanks for putting that together, @omacranger! I hadn't quite found the time, yet. |
This issue is caused by Eloquent, Lighthouse does not do anything extraordinary with the query builder. See laravel/framework#4962 for more information. The proposed fix/workaround can only be done if the user is able to control the |
I did try my hand at fixing this within Eloquent itself, not quite there yet: https://github.com/spawnia/laravel-framework/tree/prefer-original-table-columns-on-join Fixing this within Lighthouse seems to have it's own set of problems. Subqueries for relationships seem to not work correctly yet, see #1253 |
Describe the bug
I have the following type:
User.donations
is ahasManyThrough
relationship. So in order to limit byteamId
an additional join is applied during the scope:This is where things break. The resulting donations have much of the correct data but the wrong ID. The reason for this is actually pretty simple. By default selects in Laravel are
*
, which means that all columns including the joined table columns are included. Since the joined table also has an ID column and it comes later in the results the ID of the joined table item is returned instead of the correct table's ID.Expected behavior/Solution
Since Lighthouse has the model and is building the query, all we should really need to do here is to apply the table to the query select. Something like
$query->select("{$model->getTable()}.*")
.I don't believe this would cause backwards-incompatibility as the GraphQL object is expecting to only be a single model, anyway. The only scenarios I'm not sure of is how Lighthouse handles sub-models and eager loading.
Environment
Lighthouse Version: 5.9
Laravel Version: 6.x
The text was updated successfully, but these errors were encountered: