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
Question: QueryBuilder how to join on id columns and a column with arbitrary value? #436
Comments
Theres not really a simple way to do this with a query builder directly. Using models is a little easier as you can just do: User.join_on('posts', lambda q: (
q.where('active', 1)
)) This does a little bit of Kung Fu under the hood though. I can see how much easier it would be to do this without models and directly with the query builder. Maybe an interface like: builder.left_join('table1', 'table2.id', '=', 'table1.table_id', lambda query: (
query.where('contacts.user_id', '>', 5)
)) Would something like that work? |
@josephmancuso Yeah the lambda option is good and it helps encapsulate the additional column criteria. Another option where the lambda parameter is instead have an optional list of tuples builder.left_join('table1', 'table2.id', '=', 'table1.table_id', [('contacts.user_id', '>', 5), ('company.agent_id', '=', '2') ]) which feels more intuitive to me. WDYT? |
Bump @josephmancuso |
One solution is to use a new clause = (
JoinClause("report_groups as rg")
.on("bgt.fund", "=", "rg.fund")
.on("bgt.dept", "=", "rg.dept")
.on("bgt.acct", "=", "rg.acct")
.on("bgt.sub", "=", "rg.sub")
)
query.join(clause) |
Apologies for the lack of response on this .... Life is busy ATM ;) This JoinClause looks to be ideal! I'm assuming that as per my original question it will be able to handle arbitrary values? |
As in where clauses? Yes. #471 |
Nice work! |
Describe the feature as you'd like to see it
I have a requirement that a join be done on a foreign key and the value of another column.
example query:
This will produce rows where only the groupx_name columns are filled when the join for that item is fully realised
example results
In Orator you can do it like this example
Its obviously not the same as above query but you get the idea
How do we do this in Masonite Orm?
I am tempted to build a raw query but that would become fragile and I would like to know how to do this properly as the join documentation does not have any info for this common usage.
Help and pointers would be appreciated.
Thanks
The text was updated successfully, but these errors were encountered: