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

Conversation

Projects
None yet
6 participants
@staudenmeir
Contributor

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

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

This comment has been minimized.

Member

laurencei commented Nov 29, 2018

Your on fire 🔥

@ankurk91

This comment has been minimized.

Contributor

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
Details
continuous-integration/travis-ci/pr The Travis CI build passed
Details
@taylorotwell

This comment has been minimized.

Member

taylorotwell commented Nov 29, 2018

Thanks

@aaronkorn

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
$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

This comment has been minimized.

Member

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