Objection.js: joinRelation() of a m2m relation with "extra" properties fails when relation's "modify" uses an extra

Created on 11 Mar 2018  路  8Comments  路  Source: Vincit/objection.js

it's probably a followup of #760 and connected to changes from that time.

Person model has "movies" relation with extra: 'awesomeness' and orderBy('awesomeness', 'desc') in modify property.

await Person.query()
    .joinRelation('movies')

generates this sql:

select `Person`.* from `Person` inner join `Person_Movie` as `movies_join` on `movies_join`.`personId` = `Person`.`id` inner join (select `Movie`.* from `Movie` order by `awesomeness` desc) as `movies` on `movies_join`.`movieId` = `movies`.`id`

"awesomeness" column is inserted in wrong place, causing the following error:

Error: ER_BAD_FIELD_ERROR: Unknown column 'awesomeness' in 'order clause'

Tested with v1.0.
reproduction-template.js attached.
reproduction-template.zip

bug

Most helpful comment

Actually fixing this seems to be quite challenging. The fix will probably be a breaking change and will need to wait for 2.0.

All 8 comments

Thanks for the issue and reproduction! I'll fix this for the next patch release.

Actually fixing this seems to be quite challenging. The fix will probably be a breaking change and will need to wait for 2.0.

whoops...

whoops?

I only mean i was not aware that fixing this would require that big changes, sorry. Does the issue go down to operation hooks? Or is it something else? Maybe I could help somehow?

The change is not big, but moving the modify call to the right place will break some other queries. The only fix that wouldn't break things would be to copy the pivot table join to both queries. Something like this:

select `Person`.* from `Person` 
inner join `Person_Movie` as `movies_join` on `movies_join`.`personId` = `Person`.`id` 
inner join (
  select `Movie`.* from `Movie` 
  inner join `Person_Movie` as `person_join` on `person_join`.`movie_id` = `Movie`.`id` 
  order by `awesomeness` desc
) as `movies` on `movies_join`.`movieId` = `movies`.`id`

but that's somewhat stupid and inefficient.

@falkenhawk Would you be ok with solving this by adding a modify/filter hook for the pivot table as suggested by @keepcosmos in #1356?

Yes! That'd be perfect.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

bsdo64 picture bsdo64  路  3Comments

zuck picture zuck  路  4Comments

chen7david picture chen7david  路  3Comments

officer-rosmarino picture officer-rosmarino  路  4Comments

louis-etne picture louis-etne  路  4Comments