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

[5.7] Fix self-referencing HasManyThrough existence queries #26662

Merged
merged 1 commit into from
Nov 29, 2018
Merged

[5.7] Fix self-referencing HasManyThrough existence queries #26662

merged 1 commit into from
Nov 29, 2018

Conversation

staudenmeir
Copy link
Contributor

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');
    }
}

User::has('teamMates')->get();
# 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.

@laurencei
Copy link
Contributor

Your on fire 🔥

@ankurk91
Copy link
Contributor

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

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

Thanks

@aaronkorn
Copy link

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
$table->increments('id');
$table->timestamps();
$table->unsignedInteger('sub_category_id');
MODEL: return $this->belongsTo(SubCategory::class);

SubCategory Table
$table->increments('id');
$table->timestamps();
MODEL: return $this->hasMany(Transaction::class);

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

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.

@driesvints
Copy link
Member

@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
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants