-
Notifications
You must be signed in to change notification settings - Fork 11.1k
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
Unnecessary queries when using HasOneOrMany
when primary key is null
#51266
Comments
I'm sorry but I'm afraid I don't understand this use case or see a reason why you'd ever want to use a query like this? How would the relation ever be fetched if the primary key of the parent is |
Closing this issue because it's inactive, already solved, old or not relevant anymore. Feel to open up a new issue if you're still experiencing this. |
@driesvints I apologize for not replying sooner; it's not that you want a model with a primary key that's |
I'm sorry. I just don't understand why you want to do |
@driesvints Let me give the full example, so you hopefully get a better understanding: Simple breakdown of the Employer model: <?php
namespace App\Models;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Eloquent\Model;
class Employer extends Model
{
protected $appends = [‘total_children’];
public function getTotalChildrenAttribute(): int
{
return (int) $this->children()->count();
}
public function children(): HasMany
{
return $this->hasMany(Employer::class, 'parent_id', 'id');
}
} Simple snippet representing the code that gets the filter: public function getFilters(): array
{
return [
‘employers’ => Employer::select(‘id as value’, ‘company_name as text’)->get()->toArray(),
];
} This simple scenario creates an impossible query, simply because using the select removes the expected primary key, and using In any normal scenario, there wouldn’t be an issue, but this scenario requires the filter value to be labeled Yes, I want to note that performance wise this While I would open a PR if I could, I simply do not have enough knowledge of the Query Builder to know at which point it would be best to implement a bail if it creates an impossible query. |
I'm sorry but I just don't think we can ever support that scenario. A primary identifier shouldn't be aliased like that in a query. |
@driesvints That's a shame. I just tested and confirmed that it also happens on a replicated instance (which makes sense). I suppose I'll try and see if I can make a PR. Either way, I guess for performance it's important to keep this in mind. |
Laravel Version
11.6.0
PHP Version
8.2.5
Database Driver & Version
MySQL 8.0.32
Description
When using
HasMany
orMorphMany
relations, if the model's primary key isnull
, it creates an impossible query:In the above scenario, the employer model utilized has been fetched using a
select
statement which transforms the primary key (select('id as value')
). However, when attempting to fetch relations on this model, it will still call the database.Seemingly, this should have been fixed with #26992 but I suppose when it is a direct relation query it doesn't use
getResults
and therefore doesn't bail.Steps To Reproduce
parent_id
:select
and call the relation as query:The text was updated successfully, but these errors were encountered: