Skip to content
This repository was archived by the owner on Jul 21, 2019. It is now read-only.

Model Ambiguity? #13

@gkoberger

Description

@gkoberger

Maybe I'm missing something, however what happens if a Schema has two fields of the same model? For example, taking the example from the main site, let's say we also want to keep track of an editor.

var mongoose = require('mongoose');
require('mongo-relation');

// UserSchema stores an Array of ObjectIds for posts
var UserSchema = new mongoose.Schema({
    posts: [mongoose.Schema.ObjectId],
    edited: [mongoose.Schema.ObjectId]
});

// PostSchema stores an ObjectId for the author
var PostSchema = new mongoose.Schema({
    title  : String
  , author : mongoose.Schema.ObjectId
  , editor : mongoose.Schema.ObjectId
});

// Attach the plugin
UserSchema.hasMany('Post', {through: 'posts'});
UserSchema.hasMany('Post', {through: 'edited'});

PostSchema.belongsTo('User', {through: 'author'});
PostSchema.belongsTo('User', {through: 'editor'});

var User = mongoose.model('User', UserSchema)
  , Post = mongoose.model('Post', PostSchema);

Am I missing something? Or can two models only be associated once?

Thanks!

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions