Skip to content

Fields in .select() clause repeated in JoinEagerAlgorithm #256

@jeff-kilbride

Description

@jeff-kilbride

I've found something interesting using the JoinEagerAlgorithm. I have a Video model with the following:

    static get defaultEagerOptions() {
        return {
            aliases: {
                'bucket': 'b'
            }
        };
    }

    static get relationMappings() {
        return {
            bucket: {
                relation: Model.BelongsToOneRelation,
                modelClass: 'Bucket',
                join: {
                    from: 'videos.bucket_id',
                    to: 'buckets.id'
                }
            }
        };
    }

I ran this query:

Video.query()
  .findById(1518)
  .select('videos.id', 'videos.price', 'videos.created', 'videos.updated')
  .eager('bucket')
  .modifyEager('bucket', builder => builder.select('name'))
  .debug()
  .then(console.log);

And I got this generated SQL:

select `videos`.`id`, 
  `videos`.`price`, 
  `videos`.`created`, 
  `videos`.`updated`, 
  `videos`.`id` as `id`, 
  `videos`.`created` as `created`, 
  `videos`.`updated` as `updated`, 
  `videos`.`price` as `price`, 
  `b`.`id` as `b:id`, 
  `b`.`name` as `b:name` 
from `videos` as `videos` 
  left join (select `name`, `id` from `buckets`) as `b` 
    on `b`.`id` = `videos`.`bucket_id` 
where `videos`.`id` = ?

Notice the fields from my .select() method are repeated with the as clause added. However, the data returned was correct:

Video {
  id: 1518,
  price: 9.99,
  created: 2016-06-30T12:53:22.000Z,
  updated: 2016-12-01T12:28:08.000Z,
  bucket: Bucket { name: 'vcs-9-1' } 
}

Out of curiousity, I tried the following:

Video.query()
  .findById(1518)
  .select(
    'videos.id as id', 
    'videos.price as price', 
    'videos.created as created', 
    'videos.updated as updated'
  )
  .eager('bucket')
  .modifyEager('bucket', builder => builder.select('name'))
  .debug()
  .then(console.log);

This time, only the videos.id field was repeated in the generated SQL:

select `videos`.`id` as `id`, 
  `videos`.`price` as `price`, 
  `videos`.`created` as `created`, 
  `videos`.`updated` as `updated`, 
  `videos`.`id` as `id`, 
  `b`.`id` as `b:id`, 
  `b`.`name` as `b:name` 
from `videos` as `videos` 
  left join (select `name`, `id` from `buckets`) as `b` 
    on `b`.`id` = `videos`.`bucket_id` 
where `videos`.`id` = ?

But the videos.id field was missing from the returned data:

Video {
  price: 9.99,
  created: 2016-06-30T12:53:22.000Z,
  updated: 2016-12-01T12:28:08.000Z,
  bucket: Bucket { name: 'vcs-9-1' } 
}

Let me know if there is any more info I can provide that would help. Thanks!

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions