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

'whereHas' using wrong table name when relationship refers to self #42075

Closed
tiagof opened this issue Apr 21, 2022 · 13 comments
Closed

'whereHas' using wrong table name when relationship refers to self #42075

tiagof opened this issue Apr 21, 2022 · 13 comments

Comments

@tiagof
Copy link
Contributor

tiagof commented Apr 21, 2022

  • Laravel Version: 9.1.0
  • PHP Version: 8.1
  • Database Driver & Version: Mysql

Description:

When using whereHas on a Model that implements SoftDeletes and has a relationship to itself, the generated query is wrong.

Steps To Reproduce:

Create 2 models like this

class Client extends Model {
  use SoftDeletes;
}

class Contract extends Model {
  use SoftDeletes;

  public function relationSelf(): HasMany
  {
      return $this->hasMany(static::class)->onlyTrashed();
  }

  public function relationOther(): HasMany
   {
      return $this->hasMany(Client::class)->onlyTrashed();
   }
}
➜ art --version
Laravel Framework 9.1.0

➜ art tinker
Psy Shell v0.11.1 (PHP 8.1.4 — cli) by Justin Hileman

>>> Contract::whereHas('relationSelf')->toSql();
[!] Aliasing 'Contract' to 'Domains\Contracts\Models\Contract' for this Tinker session.
=> "select * from `contracts` where exists (select * from `contracts` as `laravel_reserved_0` where `contracts`.`id` = `laravel_reserved_0`.`contract_id` and `contracts`.`deleted_at` is not null) and `contracts`.`deleted_at` is null"

>>> Contract::whereHas('relationOther')->toSql();
=> "select * from `contracts` where exists (select * from `clients` where `contracts`.`id` = `clients`.`contract_id` and `clients`.`deleted_at` is not null) and `contracts`.`deleted_at` is null"
>>>

Notice that in the first instruction, we have contracts.deleted_at is not null when I believe we should get laravel_reserved_0.deleted_at is not null.
The complete correct sql query would be
select * from `contracts` where exists (select * from `contracts` as `laravel_reserved_0` where `contracts`.`id` = `laravel_reserved_0`.`contract_id` and `laravel_reserved_0`.`deleted_at` is not null) and `contracts`.`deleted_at` is null

In the second instruction, because this relationship refers to another Model, the query is generated correctly.

@driesvints
Copy link
Member

Can you try the very latest Laravel version? This is already fixed I think.

@tiagof
Copy link
Contributor Author

tiagof commented Apr 21, 2022

Don't think it is ...

site on  upg/laravel9 [!] via ⬢ v17.7.2 via 🐘 v8.1.4 took 11s
➜ art --version
Laravel Framework 9.9.0

site on  upg/laravel9 [!] via ⬢ v17.7.2 via 🐘 v8.1.4
➜ art tinker
Psy Shell v0.11.2 (PHP 8.1.4 — cli) by Justin Hileman
>>> Contract::whereHas('relationSelf')->toSql();
[!] Aliasing 'Contract' to 'Domains\Contracts\Models\Contract' for this Tinker session.
=> "select * from `contracts` where exists (select * from `contracts` as `laravel_reserved_0` where `contracts`.`id` = `laravel_reserved_0`.`contract_id` and `contracts`.`deleted_at` is not null) and `contracts`.`deleted_at` is null"
>>> Contract::whereHas('relationOther')->toSql();
=> "select * from `contracts` where exists (select * from `clients` where `contracts`.`id` = `clients`.`contract_id` and `clients`.`deleted_at` is not null) and `contracts`.`deleted_at` is null"
>>>

EDIT:

At some point in the query generation it is correct
Screenshot 2022-04-21 at 12 27 09

but then the mergeConstraintsFrom function messes it up:
Screenshot 2022-04-21 at 12 28 41

@driesvints
Copy link
Member

Does it work if you do $this->hasMany(self::class)->onlyTrashed();?

@tiagof
Copy link
Contributor Author

tiagof commented Apr 21, 2022

Same result

@driesvints
Copy link
Member

Heya, thanks for reporting.

We'll need more info and/or code to debug this further. Can you please create a repository with the command below, commit the code that reproduces the issue as separate commits on the main/master branch and share the repository here? Please make sure that you have the latest version of the Laravel installer in order to run this command. Please also make sure you have both Git & the GitHub CLI tool properly set up.

laravel new bug-report --github="--public"

Please do not amend and create a separate commit with your custom changes. After you've posted the repository, we'll try to reproduce the issue.

Thanks!

@tiagof
Copy link
Contributor Author

tiagof commented Apr 21, 2022

Sure, i'll do that, but again, the 2 classes in here
are enough.

tiagof added a commit to tiagof/bug-report that referenced this issue Apr 21, 2022
@tiagof
Copy link
Contributor Author

tiagof commented Apr 21, 2022

@driesvints
Copy link
Member

@tiagof can you add a test for me to run to reproduce the output?

@tiagof
Copy link
Contributor Author

tiagof commented Apr 21, 2022

Added.
(testing the output of the toSql() method, otherwise would need to create migrations and populate the DB)

EDIT: art test --filter BugTest

@taylorotwell
Copy link
Member

taylorotwell commented Apr 21, 2022

It's because SoftDeletingScope simply isn't built to handle self-referencing relationships and aliases. The framework would need a PR to enable this behavior.

CleanShot 2022-04-21 at 10 45 48@2x

It's basically same situation as:

return $this->hasMany(static::class)->where('foo', 'bar');

This also generates the "wrong query" with whereHas + self-referential relations. ^

@tiagof
Copy link
Contributor Author

tiagof commented Apr 21, 2022

I see. The relationship itself works fine:
Contract::find(1)->relationSelf
returns the expected related models.

It happens as you say, only when alias are involved.

I'll see if I can find a fix but the internals of Eloquent don't look so easy :)

@driesvints
Copy link
Member

Seems like this indeed isn't supported right now. Like Taylor said, we'd appreciate PR's for this. Thanks

@tiagof
Copy link
Contributor Author

tiagof commented Apr 22, 2022

@driesvints: PR submitted. #42100
Maybe there is a better way to achieve this, but this one seems to work.

Hope you can review it

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

No branches or pull requests

3 participants