Vincit / objection.js

An SQL-friendly ORM for Node.js
https://vincit.github.io/objection.js
MIT License
7.26k stars 639 forks source link

Id property missing after specific query builder combination #2288

Open stetttho opened 2 years ago

stetttho commented 2 years ago

Not sure if I'm doing something wrong or if this is a bug. The id property was missing on the query result for the following query:

const collectionsQuery = Collection.query()
     .select( 
        'Collection.*',  
        Collection.relatedQuery('collectible')
          .count()
          .as('numberOfCollectibles')
      )
      .withGraphFetched('[previewMedia, additionalInfo]')

it was working fine before, the thing I changed was to add a second entry in "withGraphFetched".

Workaround for now:

const collectionsQuery = Collection.query()
      .select(
        'Collection.*',
        Collection.relatedQuery('collectible')
          .count()
          .as('numberOfCollectibles')
      )
      .withGraphFetched('[previewMedia]')
      .withGraphJoined('[additionalInfo]')

I'm using the latest release (3.0.1)

soulsam480 commented 2 years ago

I'm also having a similar issue,

await DemoFrame.query()
        .where('demoFrames.demoId', request.params.id)
        .leftJoin('frames as f', 'f.id', 'demoFrames.frameId')
        .select(
          'demoFrames.*',
          'f.pageUrl as pageUrl',
        )
        .withGraphFetched('hotspots')

id is missing when adding .withGraphFetched('hotspots'), its' there without .withGraphFetched('hotspots')

lehni commented 1 year ago

Could one of you provide a simple reproducible example using the reproduction-template.js in the objection repo? This would help a lot with debugging and fixing this issue.

lehni commented 1 year ago

This might be a duplicate of #2219

itskemo commented 11 months ago

I'm on 3.1.3 and this issue got me stuck on production code. What's more surprising is that when trying to reproduce this on suggested reproduction-template.js the problem is not occurring. As suggested above, using withGraphJoined did actually return id while withGraphFetched did not.

EDIT: Using the first example in this thread in order to explain what's going on.

Looks like problem is that Collection have id column and additionalInfo have one as well, causing the id column from Collection not being returned. Something like following should work, question is if this is expected behavior:

const collectionsQuery = Collection.query()
     .select( 
        'Collection.*',
        'Collection.id as collectionId',  // renaming `id` to `collectionId` would return the `id` as `collectionId`
        Collection.relatedQuery('collectible')
          .count()
          .as('numberOfCollectibles')
      )
      .withGraphFetched('[previewMedia, additionalInfo]')

I have tried to rename the additionalInfo.id to something else, but it did not help. Is this expected behavior @lehni ?

Again, not reproducible on the suggested template, however works on the production code.

pawel-krzyzak commented 1 month ago

I'm having a similar issue as well. I guess it has something to do with the table names consisting of multiple words (containing "_"). Tried debugging and found that when using .select('tableName.*') with .withGraphFetched() objection produces query that has both "table_name.*" and "table_name"."external_id" in the select statement and the externalId property is missing. The problem does not occur when not using the .select() method (externalId is present).

i.e.:

await this.dataExtensionModel
    .query()
    .withGraphFetched('businessUnit')
    .debug();

produces:

select "data_extension".* from "data_extension"

but:

await this.dataExtensionModel
    .query()
    .select('dataExtension.*')
    .withGraphFetched('businessUnit')
    .debug();

produces:

select "data_extension".*, "data_extension"."business_unit_id" from "data_extension"

for very similar models and relations, whose names don't consist of two words, everything works as expected.