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

Nested queries setting a filter does not convert the Subquery to a JOIN #391

Closed
MrSwitch opened this issue Jan 22, 2025 · 1 comment
Closed
Assignees

Comments

@MrSwitch
Copy link
Member

When requesting fields from another model, a subquery is typically used when there is no filter provided. If however the handler then defines the filter, the nested model join is not converted from a Subquery to a JOIN, causing SQL errors. Tables referenced within a subquery are not available outside of that subquery, which is why we need regular JOINs.

dare.options.models.nestedTable = {
   get(options) {
       options.filter = {is_deleted: 0}; // should have been a `join` not a `filter`
   }
};


const query = {
   // ..
  fields: ['field', {nestedTable: ['field']]
};

Will produce SQL

SELECT ..., (SELECT JSON_OBJECT('field', b.field) FROM nestedTable b WHERE a.id = b.a_id) as nestedTable
FROM ...
WHERE b.is_deleted = 0
-- Error: Unknown column 'b.is_deleted' in 'where clause'

Approach
Allow setting of the filter in nested fields, but convert the request into a JOIN if it is not already.

@MrSwitch MrSwitch self-assigned this Jan 22, 2025
5app-Machine added a commit that referenced this issue Jan 23, 2025
## [0.92.1](v0.92.0...v0.92.1) (2025-01-23)

### Bug Fixes

* **handler:** enable handlers to attach filters to the request, fixes [#391](#391) ([#394](#394)) ([d6323b7](d6323b7))
@5app-Machine
Copy link
Contributor

🎉 This issue has been resolved in version 0.92.1 🎉

The release is available on:

Your semantic-release bot 📦🚀

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

No branches or pull requests

2 participants