Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

error on eager loading #64

Closed
devashishsethia opened this issue Oct 2, 2019 · 2 comments
Closed

error on eager loading #64

devashishsethia opened this issue Oct 2, 2019 · 2 comments

Comments

@devashishsethia
Copy link

Hi,

I've the following relations:

class shifts extends Model {

  static get tableName() {
    return 'shifts';
  }

  static get jsonSchema() {
    return {
      type: 'object',
      required: ['name'],

      properties: {
        name: { type: 'string' },
        date: {type: 'date'}
      }
    };
  }

  static get relationMappings() {
    const Slot = require('./slots.model')
    return {
      slots: {
        relation: Model.HasManyRelation,
        modelClass: Slot,
        join: {
          from: 'shifts.id',
          to:'slots.shiftId'
        }
      }
    }
  }

  $beforeInsert() {
    this.createdAt = this.updatedAt = new Date().toISOString();
  }

  $beforeUpdate() {
    this.updatedAt = new Date().toISOString();
  }
}
class slots extends Model {

  static get tableName() {
    return 'slots';
  }

  static get jsonSchema() {
    return {
      type: 'object',
      required: ['startAt', 'endAt', 'availableOn'],

      properties: {
        startAt: { type: 'time' },
        endAt: {type: 'time'},
        availableOn: {type: 'number'},
        shiftId: {type:'number'}
      }
    };
  }

  static get relationMappings() {
    const Shift = require('./shifts.model')
    return {
      shift: {
        relation: Model.BelongsToOneRelation,
        modelClass: Shift,
        join: {
          from: 'slots.shiftId',
          to: 'shifts.id'
        }

      }
    };
  }

  slotDisplayName() {
    return (moment(this.startAt, "hh:mm:ss").format('h:mm a') + ' - ' + moment(this.endAt,"hh:mm:ss").format('h:mm a'));
  }

  static get virtualAttributes() {
    return ['slotDisplayName'];
  }

  $beforeInsert() {
    this.createdAt = this.updatedAt = new Date().toISOString();
  }

  $beforeUpdate() {
    this.updatedAt = new Date().toISOString();
  }
}

but when I do the following :

await this.app.service('slots').Model.query().eager('shift');
or
await this.app.service('shifts').Model.query().eager('slots');

I get the following errors:
"slots.relationMappings.shift: Cannot read property 'get' of undefined"
"shifts.relationMappings.slots: Cannot read property 'get' of undefined"

Any clue why this could be happening? I'm using feathers-objection and have whitelisted eager.

I opened an issue on the objections github page but sami asked me to open an issue here as my model looks ok and it could be a but in feathers-objection.

@devashishsethia
Copy link
Author

If I handle the migrations separately and only export model class from the generated shifts.model.js and slots.model.js file, then it seems to work fine.
Essentially, only leave the following code at the end of the file.
module.exports = function (app) {
return shifts;
};
I'm closing the issue as it's resolved for me.

@ramsestom
Copy link

This is actually a serious issue and I don't know why it was closed and never fixed.
migration should not be exported with the model and should be defined in his own file.
Exporting migration with the model is not only dangerous but it also break objectionjs on some points like relation mappings...
So feathers-objection should define models and migrations in two different files and only export the model in the model class file

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants