Objection.js: Subqueries break upgrading from 1.4.0 to 1.5.3

Created on 25 Jan 2019  路  3Comments  路  Source: Vincit/objection.js

Greetings,

Subqueries using .with are no longer working as expected when I upgrade from 1.4.0 to 1.5.3.

  return Model
    .query()
    .with(
      'pager_subquery',
      query
    )
    .count()
    .from('pager_subquery')
    .first();

The above code takes a query, which is an objection model .query() and uses it to construct a subquery using .with to return a basic row count.

Under 1.4.0, the following correct SQL is generated:

with "pager_subquery" as (select "posts".* from "posts" where "posts"."postable_id" in ($1) and "postable_type" = $2 and "posts"."deleted_at" is null order by "stats"#>'{scores,low}' desc) select count(*) from "pager_subquery"

Under 1.5.3, the following incorrect SQL is generated:

with "pager_subquery" as (select "pager_subquery".* from "pager_subquery" where "pager_subquery"."postable_id" in ($1) and "postable_type" = $2 and "posts"."deleted_at" is null order by "stats"#>'{scores,low}' desc) select count(*) from "pager_subquery"

It appears that under 1.5.3, the original query object loses its model tablename and/or is overridden by the .from('pager_subquery') method call.

I had a look through the change log and couldn't find anything that indicates that this behaviour has changed.

Is this a bug or has Objection changed this behaviour? If the latter, any hints or clues would appreciated.

Libraries:

  • knex: 0.16.3
  • pg: 7.8.0
  • db: Postgres 10.5

Most helpful comment

Thank you for this @koskimas! And super thanks for objection.js - words cannot describe how much I 鉂わ笍it and how pleasurable it is to use.

All 3 comments

This is probably a bug in objection. I'm on vacation for three weeks. I'll look into this after I'm back

Thank you for this @koskimas! And super thanks for objection.js - words cannot describe how much I 鉂わ笍it and how pleasurable it is to use.

objection 1.6 is now released with a fix for this issue.

Was this page helpful?
0 / 5 - 0 ratings