Automattic / Automattic/mongoose

Can't use same discriminator in multiple models

Open
#7,422 7 comments 0 reactions 0 assignees View on GitHub
discussion
Dominant language
JavaScript
Stars
27.5k
Forks
4k
Avg merge
2d 7h
Merged PRs (30d)
35

Description

**Do you want to request a *feature* or report a *bug*?**
To me it seems like a bug, but probably it will end up being a feature request.

**What is the current behavior?**
Trying to define the same discriminator for two different models on two different collections throws an `OverwriteModelError: Cannot overwrite `[model name]` model once compiled.`

**If the current behavior is a bug, please provide the steps to reproduce.**`
```
const mongoose = require('mongoose');
const { Schema } = mongoose;
const DB = '7100';
const URI = `mongodb://localhost:27017/${DB}`;
const OPTS = { family: 4, useNewUrlParser: true };

const DocumentSchema = new Schema({
});

const InvoiceSchema = new Schema({
});

const DeliveryNoteSchema = new Schema({
});

const FactSchema = new Schema({
});

const InvoiceFactSchema = new Schema({
});

const DeliveryNoteFactSchema = new Schema({
});

const Document = mongoose.model('Document', DocumentSchema);
const Invoice = Document.discriminator('Invoice', InvoiceSchema);
const DeliveryNote = Document.discriminator('DeliveryNote', DeliveryNoteSchema);

const Fact = mongoose.model('Fact', FactSchema);
const InvoiceFact = Fact.discriminator('Invoice', InvoiceFactSchema);
const DeliveryNoteFact = Fact.discriminator('DeliveryNote', DeliveryNoteFactSchema);

mongoose.connect(URI, OPTS);
```

**What is the expected behavior?**
No error

**Please mention your node.js, mongoose and MongoDB version.**
node v8.10.0
mongoose 5.3.2
MongoDB 3.2

Contributor guide

Open the contributing guide

Research direction

Start by running the supplied JavaScript reproduction with Mongoose, focusing on mongoose.model() and Model.discriminator() for the two model hierarchies. Trace where discriminator names are registered and where OverwriteModelError is raised; done means both hierarchies can use the same discriminator names without the error, with regression coverage for the reported case.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript, mongodb, node.js
Domain
backend, database
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.