objection.js icon indicating copy to clipboard operation
objection.js copied to clipboard

Id property missing after specific query builder combination

Open stetttho opened this issue 2 years ago • 5 comments

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)

stetttho avatar Jun 16 '22 14:06 stetttho

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')

soulsam480 avatar Jul 28 '22 11:07 soulsam480

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 avatar Apr 15 '23 07:04 lehni

This might be a duplicate of #2219

lehni avatar Apr 15 '23 07:04 lehni

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.

itskemo avatar Dec 12 '23 14:12 itskemo