Mongoose: Error is thrown when base model's schema specifies options that are not reflected in discriminator model's schema

Created on 4 Dec 2013  路  11Comments  路  Source: Automattic/mongoose

When using the new discriminator models, mongoose throws an error, if the base model's schema specifies schema options that are not reflected in the discriminator model's schema.

Here is a simple code snippet that shows the issue:

var BaseSchema = new mongoose.Schema({
    "name": String
}, {
    "versionKey": false
});

var DiscriminatorSchema = new mongoose.Schema({
    "something": String
});

var Base = mongoose.model("Base", BaseSchema);
var Discriminator = Base.discriminator("Discriminator", DiscriminatorSchema);

The following error is thrown:

Error: Discriminator options are not customizable (except toJSON & toObject)

I understand that you can't set schema options on a schema used in a discriminator model, but it should not be an issue the other way around. I would expect the schema options of the base schema to be "inherited" by the discriminator schema.

enhancement

All 11 comments

+1

Same thing here, and agree with @flore2003...

+1

I run into this too, +1

+1

It looks like such a behavior is only for a subset of options. Not all.

var options = { discriminatorKey: 'type', timestamps: true }; gives me this error. Luckily I can just hardcode the timestamps, but it would be nice if this was supported.

Actually this looks like a dup of #3414, which means it should be fixed when 4.5 lands.

Was this never fixed? Seeing this in 4.10.6.

@ksloan this particular issue is fixed. If you're experiencing a similar issue, please open up a new issue with a repro script.

fwiw, I see the same issue in 4.11.14 when my base schema options contains

{ "versionKey": false }

Removing it makes the error go away (because now both schemas default to "__v"

Perhaps my scenario is slightly different, but I don't understand how the original issue was fixed.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

lukasz-zak picture lukasz-zak  路  3Comments

efkan picture efkan  路  3Comments

adamreisnz picture adamreisnz  路  3Comments

simonxca picture simonxca  路  3Comments

jeneser picture jeneser  路  3Comments