Sign in
Log inSign up

Need help with Bookshelf.js polymorphic relations

Ashish Kumar's photo
Ashish Kumar
·Nov 7, 2016

I have a model AccessToken which belongs to multiple models using the keyable_id and keyable_type property. I need to access access_tokens using the associated accounts.

Here is what my AccessToken model looks like:

module.exports = baseModel =>
  baseModel.extend({
    tableName: 'access_tokens',
    keyable() {
      return this.morphTo('keyable', 'Account', 'Project', 'User');
    }
  });

Here is what my Accounts model looks like:

module.exports = baseModel =>
  baseModel.extend({
    tableName: 'accounts',
    accessTokens() {
      return this.morphMany('AccessToken', 'keyable');
    }
  });

But all I get is an empty array for access_tokens. Does anyone know how to go about to solve this?