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

OverwriteModelError: Cannot overwrite X model once compiled. #1867

Closed
2 of 4 tasks
omarberrami opened this issue Jul 19, 2023 · 2 comments
Closed
2 of 4 tasks

OverwriteModelError: Cannot overwrite X model once compiled. #1867

omarberrami opened this issue Jul 19, 2023 · 2 comments
Labels

Comments

@omarberrami
Copy link

Is there an existing issue for this?

  • I have searched the existing issues

Current behavior

The issue manifests when all models are in a package that we used to share our models. This package is built with 'tsc' and installed in our project.

When importing a model that is already used by another module, the first module needs to be used in the second module also, an error occure that: OverwriteModelError: Cannot overwrite SubscriberEvent model once compiled.

Example:

Module 1 : EventsModule

@Module({
  imports: [
    MongooseModule.forFeature([
      { name: MetaEvent.name, schema: MetaEventSchema },
      { name: SubscriberEvent.name, schema: SubscriberEventSchema },
    ]),
    TypeOrmModule.forFeature([Event]),
    SubscribersEventModule,
  ],
  controllers: [EventsController],
  providers: [EventsService, SubscribersEventService],
})
export class EventsModule {}

Module 2 : SubscribersEventModule

@Module({
  imports: [
    MongooseModule.forFeature([{ name: SubscriberEvent.name, schema: SubscriberEventSchema }]),
  ],
  controllers: [],
  providers: [SubscribersEventService, SubscribersEventDao],
  exports: [SubscribersEventService, SubscribersEventDao],
})
export class SubscribersEventModule {}

Minimum reproduction code

https://github.com/omarberrami/nest-mongoose-bug

Steps to reproduce

  1. pull the package
    2 . npm i
  2. npm run build
  3. install the package in your project using npm i 'path to the package'
  4. import the models in your modules example: import { Event, MetaEvent } from '@beeinn/database-models';
  5. use MongooseModule.forFeature

Expected behavior

the expected behavior is to load the models without problemes, if the model are already compiled non need to overwrite.

Package version

9.2.2 - 10.0.0

mongoose version

7.3.4

NestJS version

9.0.0

Node.js version

18.16.0

In which operating systems have you tested?

  • macOS
  • Windows
  • Linux

Other

OverwriteModelError: Cannot overwrite SubscriberEvent model once compiled.
at NativeConnection.Connection.model (/Users/x/Documents/bee/bff-it-app/node_modules/mongoose/lib/connection.js:1071:13)
at InstanceWrapper.useFactory [as metatype] (/Users/x/Documents/bee/bff-it-app/node_modules/@nestjs/mongoose/dist/mongoose.providers.js:29:42)
at Injector.instantiateClass (/Users/x/Documents/bee/bff-it-app/node_modules/@nestjs/core/injector/injector.js:354:55)
at callback (/Users/x/Documents/bee/bff-it-app/node_modules/@nestjs/core/injector/injector.js:56:45)
at processTicksAndRejections (node:internal/process/task_queues:95:5)
at Injector.resolveConstructorParams (/Users/x/Documents/bee/bff-it-app/node_modules/@nestjs/core/injector/injector.js:136:24)
at Injector.loadInstance (/Users/x/Documents/bee/bff-it-app/node_modules/@nestjs/core/injector/injector.js:61:13)
at Injector.loadProvider (/Users/x/Documents/bee/bff-it-app/node_modules/@nestjs/core/injector/injector.js:88:9)
at Injector.lookupComponentInImports (/Users/x/Documents/bee/bff-it-app/node_modules/@nestjs/core/injector/injector.js:281:17)
at Injector.lookupComponentInParentModules (/Users/x/Documents/bee/bff-it-app/node_modules/@nestjs/core/injector/injector.js:245:33)

@omarberrami
Copy link
Author

I created a pull request to fix the probleme : #1868

@kamilmysliwiec
Copy link
Member

Let's track this here #1868

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants