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

Discriminator - reUse #10931

Closed
cacothi opened this issue Oct 26, 2021 · 5 comments · Fixed by #11244
Closed

Discriminator - reUse #10931

cacothi opened this issue Oct 26, 2021 · 5 comments · Fixed by #11244
Labels
new feature This change adds new functionality, like a new method or class
Milestone

Comments

@cacothi
Copy link

cacothi commented Oct 26, 2021

Do you want to request a feature or report a bug?
Bug (?!)

What is the current behavior?
When using model.discriminator(name, schema), I get the following error when running by the second time:
Error('Discriminator with name "' + name + '" already exists');

The issue is that my code make references of schema and models dynamically, and when I try to refer a model that already contain that discriminator I get this error.

If the current behavior is a bug, please provide the steps to reproduce.

My framework is quite complex to share here, but as mentioned, I have all schemas defined, but the models are instantiated dynamically as the collection name changes depending on the request.

no typescript

What is the expected behavior?
The expected behavior is to return the discriminator that already exists. My work around was accessing the model.discriminators property to check if that specifc discriminator already exist:

		if (discriminatorType && discriminatorSchema) {

			if (model.discriminators && model.discriminators[discriminatorType])
				return model.discriminators[discriminatorType];

			return model.discriminator(discriminatorType, discriminatorSchema);
		}

What are the versions of Node.js, Mongoose and MongoDB you are using? Note that "latest" is not a version.
"mongoose": "^6.0.12",
Node: 16
MongoDB: 5.x

@Kamikadze4GAME
Copy link
Contributor

Hello @cacothi

Please provide full code example to reproduce.

@cacothi
Copy link
Author

cacothi commented Oct 26, 2021 via email

@IslandRhythms
Copy link
Collaborator

So the problem still exists but you found a workaround?

@IslandRhythms IslandRhythms added the needs clarification This issue doesn't have enough information to be actionable. Close after 14 days of inactivity label Oct 26, 2021
@cacothi
Copy link
Author

cacothi commented Oct 26, 2021 via email

@vkarpov15
Copy link
Collaborator

@cacothi sure, a flag for this would be useful. If you can put in a PR with this behavior behind a flag, that would be most appreciated 👍

@vkarpov15 vkarpov15 added new feature This change adds new functionality, like a new method or class and removed needs clarification This issue doesn't have enough information to be actionable. Close after 14 days of inactivity labels Oct 30, 2021
@vkarpov15 vkarpov15 added this to the 6.1.0 milestone Oct 30, 2021
@vkarpov15 vkarpov15 modified the milestones: 6.1.0, 6.2.0 Nov 17, 2021
vkarpov15 added a commit that referenced this issue Jan 24, 2022
Gh 10931 - `overwriteModels:true` to reuse discriminators
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
new feature This change adds new functionality, like a new method or class
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants