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

How do you ref other models? #25

Closed
Spazer9 opened this issue Jun 17, 2018 · 4 comments
Closed

How do you ref other models? #25

Spazer9 opened this issue Jun 17, 2018 · 4 comments

Comments

@Spazer9
Copy link

Spazer9 commented Jun 17, 2018

I'm trying to use virtuals but i'm getting an error:

"Cannot overwrite User model once compiled."

My model is like:

const BaseModel = use('MongooseModel')

require('./user')
require('./account')

class Membership extends BaseModel {

  static boot ({ schema }) {
    schema.virtual('user', {
      ref: 'User',
      localField: 'user_id',
      foreignField: '_id',
      justOne: true
    })

    schema.virtual('account', {
      ref: 'Account',
      localField: 'account_id',
      foreignField: '_id',
      justOne: true
    })

    this.addHook('preFind', async function(next){
      this.populate('user')
      this.populate('account')
      next()
    })
  }

  static get schemaOptions() {
    return {
      toObject: { virtuals: true },
      toJSON: { virtuals: true }
    }
  }

}

module.exports = Membership.buildModel('Membership')

I looked into some solutions online like this and but i'm not sure how I can do that with this adonis integration of mongoose.

@juampi92
Copy link
Owner

juampi92 commented Jun 18, 2018

Idk why it's saying about overriding the User model, cause it's not involved on that file it seems.

Try to debug it by logging each step, so you'll now if something is trying to redefine the User.
Maybe the problem is not with this file?

Also you could use schemaOptions to define the toObject and to JSON

static get schemaOptions() {
  return {
    toObject: { virtuals: true },
    toJSON, { virtuals: true }
  }
}

@Spazer9
Copy link
Author

Spazer9 commented Jun 18, 2018

@juampi92 Sorry I left the last line which builds the model (that exists at the end of each model)

@Spazer9
Copy link
Author

Spazer9 commented Jun 18, 2018

I seem to have fixed it by changing the last line on the required models to:

module.exports = require('mongoose').models.Account || Account.buildModel('Account')

and

module.exports = require('mongoose').models.User || Account.buildModel('User')

@juampi92
Copy link
Owner

Mmm but that does not make sense. It seems that you have to check if mongoose has already defined.
You shouldn't.

If it throws an error is because the model is being defined somewhere else, and you need to know that.
You shouldn't build a model without doing Account.buildModel('Account').

Check for a duplicated definition of buildModel('User')

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