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

Using filter on query with a join creates a duplicate join #17

Closed
TWeatherston opened this issue Jun 16, 2022 · 1 comment · Fixed by #18
Closed

Using filter on query with a join creates a duplicate join #17

TWeatherston opened this issue Jun 16, 2022 · 1 comment · Fixed by #18
Assignees

Comments

@TWeatherston
Copy link

I have these two related models eg:

class Organisation(Base):
    __tablename__ = "organisations"
    id = Column(INTEGER, primary_key=True)
    name = Column(VARCHAR(64))


class User(Base):
    __tablename__ = "users"
    id = Column(INTEGER, primary_key=True)
    name = Column(VARCHAR(64))
    email = Column(VARCHAR(64))
    organisation_id = Column(Integer, ForeignKey("organisations.id"))
    organisation = relationship("Organisation")

And then a filter for filtering users:

class UserFilter(Filter):
    name = Field()
    email = Field(lookup_operator=ContainsOperator)
    organisation = Field(field_name="organisation.name")

    class Meta:
        model = User

If I then create a query with a join and attempt to apply the filter to the query:

query = db_session.query(User).join(Organisation)
my_filter = UserFilter(data={"organisation": "test_org"}, query=query)
query = my_filter.apply()
print(str(query))

It creates this second joining of the organisations table:

SELECT 
    users.id AS users_id, 
    users.name AS users_name, 
    users.email AS users_email, 
    users.organisation_id AS users_organisation_id
FROM users 
    JOIN organisations ON organisations.id = users.organisation_id
    JOIN organisations ON organisations.id = users.organisation_id
WHERE organisations.name = ?
@elmkarami elmkarami self-assigned this Jun 16, 2022
@elmkarami elmkarami linked a pull request Jun 16, 2022 that will close this issue
@elmkarami
Copy link
Owner

elmkarami commented Jun 16, 2022

@TWeatherston thank you for reporting this issue, a fix has been released (version 1.1.3).

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 a pull request may close this issue.

2 participants