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.7] Fix self-referencing HasManyThrough existence queries #26662

merged 1 commit into from Nov 29, 2018


None yet
6 participants

staudenmeir commented Nov 29, 2018

Existence queries of self-referencing HasManyThrough relationships are incorrect:

class User extends Model {
    public function teamMates() {
        return $this->hasManyThrough(self::class, Team::class, 'owner_id');

# expected
select * from `users` where exists (
  select * from `users` as `laravel_reserved_0`
  inner join `teams` on `teams`.`id` = `laravel_reserved_0`.`team_id`
  where `users`.`id` = `teams`.`owner_id`

# actual
select * from `users` where exists (
  select * from `users` as `laravel_reserved_0`
  inner join `teams` on `teams`.`id` = `laravel_reserved_0`.`id`
  where `users`.`id` = `teams`.`owner_id`                    ^^

The tests didn't catch this because all the models have the same ids 1 and 2.


This comment has been minimized.


laurencei commented Nov 29, 2018

Your on fire 🔥


This comment has been minimized.


ankurk91 commented Nov 29, 2018

Hey @staudenmeir
Huge thanks for what you are doing for Laravel. 👍

@taylorotwell taylorotwell merged commit 2ffac16 into laravel:5.7 Nov 29, 2018

2 checks passed

continuous-integration/styleci/pr The analysis has passed
continuous-integration/travis-ci/pr The Travis CI build passed

This comment has been minimized.


taylorotwell commented Nov 29, 2018



This comment has been minimized.

aaronkorn commented Dec 9, 2018

Thanks you, that fixed I problem I was having.

Can you take a look at this Belongs To Relationship?
Laravel is creating a subcategory_id instead of a sub_category_id.

Illuminate\Database\QueryException : SQLSTATE[HY000]: General error: 1 no such column: subcategory_id (SQL: update "transactions" set "subcategory_id" = 1, "updated_at" = 2018-12-08 23:50:57 where "id" = 1)

This is the setup

Transaction Table
MODEL: return $this->belongsTo(SubCategory::class);

SubCategory Table
MODEL: return $this->hasMany(Transaction::class);

class TransactionsTableSeeder extends Seeder
     * Run the database seeds.
     * @return void
    public function run()
        factory(App\Transaction::class, 5)
        ->each(function ($transaction) {
            $subcategory = factory(App\SubCategory::class)->create();

Of course, I can change it to subcategory_id but I have other tables that follow the '_' naming convention and would break some of my hasManyThrough Relationships.


This comment has been minimized.


driesvints commented Dec 10, 2018

@aaronkorn please open an issue for this instead. Thanks!

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