Comments (10)
Hi Mike,
Sorry about the delay. One thing I see that you shouldn't be doing is joining via: id
. Try writing the relationship as:
WorkOrder.joinsTo(Property, {multiple: false});
This assumes that WorkOrder has a property named property_id
. Let me know if that helps! Should also remove the need to do a conditional join, your query looks like it should be OK even without the __is
at the end.
Best,
Jacob
from nodal.
As an aside, you should use via
for cases where you want to have different one to one relationships between two models. As an example, say you wanted WorkOrder to be associated with exactly two different Property models in semantically different ways.
For example, you're doing the join from WorkOrder to a Property via the default property_id
, but say a WorkOrder could have exactly one workshop associated with it that is also a Property in your database, you could have a workshop_id
property on your WorkOrder and then have a line in your relationships.js
file like this:
WorkOrder.joinsTo(Property, {via: 'workshop_id', multiple: false});
from nodal.
Jacob,
When I remove the "via" I end up getting the following error: "column w1.work_order_id does not exist". In this relationship the property_id column is in the work_orders table, and I need to join that property_id column back to the id column in properties, then restrict by properties.company_id.
Assuming the relationship syntax from the docs says child.joinsTo(parent), this is the relationship I added:
Property.joinsTo(WorkOrder, {multiple: false });
I am attempting to join the table "properties" to "work_orders" so that I can restrict the work_orders based on the company_id in the parent table of properties. Essentially making it so that someone in company XYZ cannot see rows from the work_orders table that are related to a property they do not own.
Thanks for the response!
from nodal.
Ah I had it backwards then... My bad. Glad I could help!
from nodal.
Jacob,
I actually didn't resolve the issue, I am still running into the error "column w1.work_order_id does not exist" after removing the via. So the original question was how to I force the relationship to use the correct columns work_orders.property_id -> properties.id?
from nodal.
Oh did
WorkOrder.joinsTo(Property, {multiple: false});
not resolve your issue? That should do it, you can do joins in queries the other way too :)
from nodal.
The system is erroring saying "message": "column w1.work_order_id does not exist". What I believe is happening is the normal join expects the parent table to use an 'id' column, and the child table would have 'parent_table_id' column, hence it's looking for 'work_order_id' in properties because the join is making the work_orders table the parent. I need to force the relationship to use 'work_orders.property_id' as the parent column, and join on the child column of 'properties.id'.
from nodal.
If WorkOrder.joinsTo(Property, {multiple: false});
isn't working for you, you could do something like this:
Property.joinsTo(WorkOrder, {via: 'id', using: 'property_id', multiple: false});
But that's pretty irregular. The using
field is meant for special cases, not joining on id
s. You shouldn't have to do that, and we should investigate why WorkOrder.joinsTo(Property, {multiple: false});
isn't working for you -- could be a Nodal bug.
from nodal.
Yes! That worked! It was the 'using' key that was missing, and now the records are returning only from the properties that are related to the correct company_id. Thank you so much!
from nodal.
Happy to help!
from nodal.
Related Issues (20)
- JSONApi support HOT 1
- orderBy Query Parameter HOT 3
- Fix COR Issues with nodal server HOT 2
- Aggregation on joined relationship column HOT 1
- Setting "global" variables from middleware HOT 1
- Callback being called multiple times in failing controller tests
- AcessToken.verify is not working
- Modelarray.from
- Suggestion for updated wiki docs content for 'Adding new fields columns to an existing model'
- Support for inet model datatype HOT 1
- Create index on multiple column HOT 1
- Multi tenant backend - how to use update and destroy? HOT 1
- nodal built on top of koa or express?
- Error: Model Property does not have relationship \"work_orders\"." HOT 5
- Support for MongoDB HOT 1
- cross-spawn-async deprecated
- What is the current development state of this project? HOT 7
- Generate int[] column type? HOT 1
- Db:migrate is ommiting one caracter in the table name for FK relations
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from nodal.