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

Can I include a property from another model?

Open TroyCoombs opened this issue 3 years ago • 0 comments

Can I include a property from another model?

Currently I have an 'events' model and everything works as expected. I have added a new 'bulk_retry_id' property to the 'event' model. I've included the filter, thought for sure that would work but nope.

I just added another relationMappings key like this,

        bulk_retry: { 
        relation: Model.HasOneThroughRelation,  
        modelClass: ObjectionBulkRetryModel, 
        join: { 
          from: 'events.id', 
          through: { 
            from: 'attempts.event_id', 
            to: 'attempts.bulk_retry_id', 
          }, 
          to: 'bulk_retries.id', 
        }, 
      },

which enables to query 'events' for a 'bulk_retry_id', and that works as well.

I want to include the 'id' column from the 'bulk_retries' table in the result set. How do I do this?

I've trued using a filter , like this, but that didn't work.

        bulk_retry: { 
        relation: Model.HasOneThroughRelation,  
        modelClass: ObjectionBulkRetryModel, 
        filter: (query) => query.select('bulk_retries.id'), 
        join: { 
          from: 'events.id', 
          through: { 
            from: 'attempts.event_id', 
            to: 'attempts.bulk_retry_id', 
          }, 
          to: 'bulk_retries.id', 
        }, 
      },

I've also tried using the extra property with no luck, like this,

bulk_retry: {
        relation: Model.HasOneThroughRelation,
        modelClass: ObjectionBulkRetryModel,
        join: {
          from: 'events.id',
          through: {
            from: 'attempts.event_id',
            to: 'attempts.bulk_retry_id',
            extra: ['bulk_retry_id'],
          },
          to: 'bulk_retries.id',
        },
      },

Both attempts do not break anything I still get results, but the bulk_retry_id property is never returned.

TroyCoombs avatar Apr 12 '22 15:04 TroyCoombs