Objection.js: Relations to the same model dissapear when unlinking

Created on 8 Jul 2019  路  7Comments  路  Source: Vincit/objection.js

When model A has two or more relations (let's call them x and y) to class B, then unlinking A.B though relation x affects existing links though y (these get unlinked too).

Here is the repo with code reproducing this bug (you can check out the only PR to see what has changed in relation to reproduction-template).
https://github.com/prk3/objection-bug-disappearing-relations

All 7 comments

If the two relations are linked through the same join table or the same foreign key, then yes, that's expected.

And please, avoid upsertGraph like the plague! I will be heavily discouraging its usage in 2.0.

If the two relations are linked through the same join table or the same foreign key, then yes, that's expected.

Even when there is an "extra" and "modify" for distinguishing links?
It's pretty obvious that there is a problem with representing relations like this. Take a look at this issue:
https://github.com/Vincit/objection.js/issues/1268

And it's not only a problem with upsertGraph. I've refactored the code so that it doesn't use upserts and the problem persists (it actually got worse, as relating appears to be broken too). You can see the code in reproduction repo.

This issue will actually be resolved by #1356

So the issue is that the modify and beforeInsert are for the related table, not for the pivot table. #1356 will make it possible to add modify and beforeInsert for the pivot table.

By the way, this does nothing:

 modify: qb => qb.relation === 'pet'

We cannot run javascript in the database. You need to use

 modify: qb => qb.where('relation', 'pet')

I need to set a value on the pivot table row when it's inserted (M:N relation). Will that be possible too? (not just querying)

@ericmuyser That's already possible. https://vincit.github.io/objection.js/recipes/extra-properties.html#join-table-extra-properties

Was this page helpful?
0 / 5 - 0 ratings