Is there any way to add filter options on ManyToMany through?
like,
goodFriends: {
relation: ManyToManyRelation,
to: XX,
from: YY,
through: {
from: 'XX.id'
to: 'YY.id',
filter: (builder) => { builder.where('relationships.type', 'good') }
}
}
Not currently, but that could be useful addition. It could be used as a solution to https://github.com/Vincit/objection.js/issues/837
This would allow N:M polymorphic relationships like in this answer: https://github.com/Vincit/objection.js/issues/19#issuecomment-389356230
But instead of the filter field being on the relation table, it would be on the join table, which is freaking awesome (and how I thought it worked until I kept running into this error and analyzed that answer further).

builder.where('relationKey', 'tags') - fails. it's trying to do this on the tags table not the nodes table.
I have a generic exclusive arc table that allows me to build relationships on the fly. I put the extra: ['relationKey'] on that relation thinking it would work. It doesn't. So I'll need a relationKey on every single table. And that row can't be re-used. And I can't really validate the data integrity since that field will get overwritten and isn't on the same table so I can't use a constraint. Sigh.

Edit: Looks like I can do 'tags_join.relationKey': 'tags' in my query and that works too.
Any updates on this?
I'm stack on the same issue, any update on this?
Any update about this issue?
Any update? This feature is needed...
I found a workaround by defining the intermediary table in my models and aliasing the fields using virtual attributes.
I have a polymorphic N:M relationship between Devices and Configurations through a ConfigurationLink pivot table.
In /models/ConfigurationLink :
config: {
relation: Model.HasOneRelation,
modelClass: Configuration,
join: {
from: 'configuration_links.configuration_id',
to: 'configurations.id'
}
},
In /models/Device.js :
config_link: {
relation: Model.HasManyRelation,
modelClass: ConfigurationLink,
join: {
from: "devices.id",
to: "configuration_links.configurable_id",
},
filter (builder) {
// Filter for polymorphic relationship
builder.where("configurable_type", "device");
builder.where("configuration_links.deleted", false);
}
},
And :
static get virtualAttributes() {
return ['config'];
}
config() {
return this.config_link.map((cl) => (
{
...cl.config,
premium: cl.premium, // Extra field fetched from the pivot table
})
);
}
Most helpful comment
Any update about this issue?