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 defining more than one self-referencing discriminated schema #15120

Closed
2 tasks done
ZachLeviPixel opened this issue Dec 19, 2024 · 0 comments · Fixed by #15142
Closed
2 tasks done

Error on defining more than one self-referencing discriminated schema #15120

ZachLeviPixel opened this issue Dec 19, 2024 · 0 comments · Fixed by #15142
Labels
confirmed-bug We've confirmed this is a bug in Mongoose and will fix it.
Milestone

Comments

@ZachLeviPixel
Copy link
Contributor

Prerequisites

  • I have written a descriptive issue title
  • I have searched existing issues to ensure the bug has not already been reported

Mongoose version

8.9.1, 8.7.3

Node.js version

18.16.0

MongoDB server version

4.8.1

Typescript version (if applicable)

No response

Description

Defining more than one discriminated schema causes an Error: Discriminator with name already exists

Steps to Reproduce

'use strict';

const mongoose = require('mongoose');
const { Schema } = mongoose;

const baseSchema = new Schema({
    type: { type: Number, required: true },
}, { discriminatorKey: "type" });

const selfRefSchema = new Schema({
    self: { type: [baseSchema] },
});

const anotherSelfRefSchema = new Schema({
    self2: { type: [baseSchema] },
});

baseSchema.discriminator(5, selfRefSchema);
baseSchema.discriminator(6, anotherSelfRefSchema); // Commenting this line make the script work
mongoose.model("base", baseSchema); // Error: Discriminator with name "5" already exists

Expected Behavior

Should be able to define any number of schemas that self reference.

@vkarpov15 vkarpov15 added this to the 8.9.4 milestone Dec 27, 2024
@vkarpov15 vkarpov15 added the has repro script There is a repro script, the Mongoose devs need to confirm that it reproduces the issue label Dec 27, 2024
@vkarpov15 vkarpov15 added confirmed-bug We've confirmed this is a bug in Mongoose and will fix it. and removed has repro script There is a repro script, the Mongoose devs need to confirm that it reproduces the issue labels Jan 1, 2025
vkarpov15 added a commit that referenced this issue Jan 2, 2025
fix(schema): allow multiple self-referencing discriminator schemas using Schema.prototype.discriminator
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
confirmed-bug We've confirmed this is a bug in Mongoose and will fix it.
Projects
None yet
2 participants