[8.x] Fix qualified column in withAggregate - #35061
Merged
Merged
Conversation
Contributor
|
@dbakan >>> Product::withMin('comments', 'id')->get()
=> Illuminate\Database\Eloquent\Collection {#3241
all: [
App\Models\Product {#4119
id: 1,
name: "fSbKRoIpBl",
created_at: "2020-11-02 09:48:39",
updated_at: "2020-11-02 09:48:39",
comments_min_id: 1,
},
App\Models\Product {#3249
id: 2,
name: "TBpEYufkJI",
created_at: "2020-11-02 09:48:39",
updated_at: "2020-11-02 09:48:39",
comments_min_id: 3,
},
App\Models\Product {#4185
id: 3,
name: "iBlkWmyfU3",
created_at: "2020-11-02 09:48:39",
updated_at: "2020-11-02 09:48:39",
comments_min_id: 7,
},
App\Models\Product {#3557
id: 4,
name: "c0o1InKesx",
created_at: "2020-11-02 09:48:47",
updated_at: "2020-11-02 09:48:47",
comments_min_id: 5,
},
],
}This is the raw SQL: >>> Product::withMin('comments', 'id')->toSql()
=> "select `products`.*, (select min(`id`) from `comments` where `products`.`id` = `comments`.`commentable_id` and `comments`.`commentable_type` = ? limit 1) as `comments_min_id` from `products`" |
Contributor
Author
|
@khalilst There is no // migration to create the role_user table:
Schema::create('role_user', function (Blueprint $table) {
$table->id(); // this will be the ambiguous column
$table->foreignId('user_id');
$table->foreignId('role_id');
$table->unique(['user_id', 'role_id']);
});
// relation on User model
public function roles()
{
return $this->belongsToMany('App\Models\Role');
}>>> User::withMin('roles', 'id')->toSql();
select `users`.*, (select min(`id`) from `roles` inner join `role_user` on `roles`.`id` = `role_user`.`role_id` where `users`.`id` = `role_user`.`user_id` limit 1) as `roles_min_id` from `users`I know that you might drop the column |
Contributor
|
@dbakan Right, thanks for clarifying. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This PR fixes a QueryException when using the new Builder features
withAggregate,withMin,withSum, etc.When using
User::withMin('roles', 'id')on abelongsToManyrelation the current implementation creates a query likewhich can cause an SQL error:
This PR passes the
$columnthrough$relation->getRelated()->qualifyColumn($column)to ensure fully qualified names: