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

Open
2 tasks done
ZachLeviPixel opened this issue Dec 19, 2024 · 0 comments
Open
2 tasks done

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.

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

1 participant