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

Many-to-many relationship joins do not use pivot table #660

Closed
stoutput opened this issue May 11, 2022 · 2 comments · Fixed by #719
Closed

Many-to-many relationship joins do not use pivot table #660

stoutput opened this issue May 11, 2022 · 2 comments · Fixed by #719
Labels
bug An existing feature is not working as intended medium These issues are geared for people who have contributed to the project before

Comments

@stoutput
Copy link
Contributor

stoutput commented May 11, 2022

Describe the bug
When using .joins() or .join_on() on a table with a predefined belongs_to_many relationship, the resulting query does not join on the pivot table.

To Reproduce

  1. Add a belongs_to_many relationship on a model:
    class Organization(Model):
        @belongs_to_many(
             local_foreign_key="organization_id",
             other_foreign_key="user_id",
             local_owner_key="organization_id",
             other_owner_key="id",
             table="organization_members",
             with_fields=["role"],
          )
          def members(self):
             from .user import User
             return User
  2. Build a joins query:
    Organization.select('users.email').joins('members').where('id', "<some_id>").to_sql()
  3. Note that the resulting SQL attempts to join directly to users:
    SELECT `users`.`email`
    FROM `organizations`
    INNER JOIN `users` ON `organizations`.`organization_id` = `users`.`user_id`
    WHERE `organizations`.`id` = '<some_id>'

Expected behavior
The resulting SQL should join to users through the defined relationship:

SELECT `users`.`email`
FROM `organizations`
INNER JOIN `organization_members` ON `organizations`.`organization_id` = `organization_members`.`organization_id`
INNER JOIN `users` ON `organization_members`.`user_id` = `users`.`id`
WHERE `organizations`.`id` = '<some_id>'

Desktop (please complete the following information):

  • OS: OSX
  • Version latest

What database are you using?

  • Type: MySQL
  • Version 5.7
  • Masonite ORM 2.10

Additional Context
The clause arg in the .joins() method should change the second join type, since the pivot join should always be INNER

@stoutput stoutput added the bug An existing feature is not working as intended label May 11, 2022
@josephmancuso josephmancuso added the medium These issues are geared for people who have contributed to the project before label May 16, 2022
@Kaweechelchen
Copy link

Is it possible that the "with_fields" option is also not working?

Your expected behaviour should also list that field as a selected column, correct?

@josephmancuso
Copy link
Member

It's possible. I think the issue here is that the BelongsToMany relationship is just not doing the relationship joining right.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug An existing feature is not working as intended medium These issues are geared for people who have contributed to the project before
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants