Join table with extra columns - Patching
#97
Any chance we could make similar changes for patching? This would simplify some code. If I add a .where() it checks the related table and not the through table. Or is there already a way to specify where() on the through table?
Example based on other post:
person
.$relatedQuery('movies')
.patch({
name: 'The room',
order: 1
})
Result:
update `Movies` set `Name` = 'The room' where `Movies`.`id` in (select `Person_Movie`.`movieId` from `Person_Movie` where `Person_Movie`.`personId` = ? and `Person_Movie`.`order` = 1)
Hmm I guess the workaround isn't too complex.
let movies = person.movies.filter(m => m.order == 1)
Movie
.query()
.whereIn('movieId', _.pluck(movies, 'movieId'))
.patch({name: 'The room'})
What version are you using? Patching with extra columns doesn't currently work, but the query I'm getting is
update `Movies` set `name` = 'The room', `order` = 1 where `Movies`.`id` in (select `Person_Movie`.`movieId` from `Person_Movie` where `Person_Movie`.`personId` = ?)
Was the Result you posted the thing you want or the thing that is currently happening?
The result was what I am looking for.
edit: version - 0.7.12
So what you actually want is this:
person
.$relatedQuery('movies')
.patch({
name: 'The room'
})
.where('order', 1)
to produce
update `Movies` set `Name` = 'The room' where `Movies`.`id` in (select `Person_Movie`.`movieId` from `Person_Movie` where `Person_Movie`.`personId` = ? and `Person_Movie`.`order` = 1)
?
In your example both order and name were in the patch object.
Unfortunately this is currently difficult to implement since objection doesn't parse where calls. It simply passes them to knex. We would need to parse the where calls and then move the ones that mention one of the extra properties to the subquery.
I will close this for now if you can live with your workaround? We may need to start parsing the where calls at some point and this will be supported after that.
I don't know if you missed it, but there was a question in my last comment 😄
Did you want to:
- update extra columns?
- or filter rows using extra columns?
Sorry, I read that you'd leave it since I had a workaround I was using and didn't reply.
Yeah I am just looking for a way to update related data using a where on the join table. My actual thing is users, user_phones and phones. My join table primary key is user_id, phone_id, type (enum: Home, Cell, etc) for fast lookups.
So I want some way to updated related phone number where the user_phones.type = 'Home'. I wrote the proposed example as I did because that's how the insert statement works with the extra field on the relation.
Ah to answer both your questions. I would like number 2 but I could see my original issue post someone could want to change the movie order as well.
Either way my work around is already in place and live so this isn't a pressing issue.
Seems like 1 would need 2 in place anyway. So you'd end up doing:
person
.$relatedQuery('movies')
.patch({
name: 'The room',
order: 2
})
.where('order', 1)
1 is actually already implemented.
Filtering using extra columns would be cool, but unfortunately it's currently too difficult to implement because of the things I explained in an earlier comment. If knex ever gets a "reflection" system that allows users to inspect the called methods (wheres, selects etc.), I'll add this. It may be never, so I'll close this for now.
Sorry if this reopens the issue. How does 1 work? Does it update all related? Wouldn't every movie be 'The room' after that and all order columns are 1?
On Tue, Aug 8, 2017, 1:45 PM Sami Koskimäki [email protected] wrote:
Closed #384 https://github.com/Vincit/objection.js/issues/384.
— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/Vincit/objection.js/issues/384#event-1197694637, or mute the thread https://github.com/notifications/unsubscribe-auth/AApwCADEcZl14WJg8pqNnftcz_KbORqeks5sWJ6YgaJpZM4Nb-iS .
You can filter the query based on all Movie columns, but not Movie_Actor (join table) columns:
person
.$relatedQuery('movies')
.patch({
name: 'The room',
order: 2
})
.where('name', 'Matrix')
.where('releaseYear', 1999)