question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Model.init() doesn't work after dropDatabase() when Model is initialized before

See original GitHub issue

In the following scenario …

const EventModel = mongoose.model('Event',
    new Schema({ eventStreamId: Schema.Types.ObjectId, eventStreamVersion: Number, /*..*/ })
        .index({ eventStreamId: 1, eventStreamVersion: 1 }, { unique: true }));

// ...

await mongoose.connect(/*...*/);

await mongoose.connection.dropDatabase();

// ...

await EventModel.init();

… it is possible that the indexes are not created after await EventModel.init(). Happens every second time on my local machine.

After moving the const EventModel = mongoose.model(...) after dropDatabase() it works.

=> It seems that although the collection and the indexes are rebuilt after dropDatabase(), the init() call doesn’t wait for the (new indexes).

mongoose: 5.2.6, mongodb: 4.0, node: 10.7

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Reactions:2
  • Comments:5

github_iconTop GitHub Comments

2reactions
ulrichbcommented, Aug 7, 2018

Hi @vkarpov15

I’m curious, why are you declaring your models before calling mongoose.connect()

That’s because my models are declared in ES module global consts next to my repositories (which is nice, because they are hidden in the modules, initialized once and accessible in all repo methods). Then I had the problem that the module initializing occurred before the data access initialization (including connect() and dropDatabase()) => The situation described above.

I fixed this issue by early initializing the data access and later initialize my container (which initializes my models). => The problem is now fixed for me.

BUT: I think issue this should still be solved, because I think this is a pitfall, and (especially when the indexes are data access-logic relevant, like unique indexes for concurrency control) and therefore a “bug waiting to happen”.

0reactions
vkarpov15commented, May 29, 2022

Turns out this was already fixed in 5.2.15 with https://github.com/Automattic/mongoose/commit/1ba6ca779126b61699eccf44516540a6ab6a75da, we just had a typo in the issue number 😦 .

Read more comments on GitHub >

github_iconTop Results From Across the Web

Forcing code-first to always initialize a non-existent database?
I'm looking to initialize the database if it doesn't exist any time I try to access it, even if that context has been...
Read more >
86. Database Initialization - Spring
Spring Boot chooses a default value for you based on whether it thinks your database is embedded. It defaults to create-drop if no...
Read more >
Database Initialization Strategies in EF 6 Code-First
DropCreateDatabaseIfModelChanges: This initializer drops an existing database and creates a new database, if your model classes (entity classes) have been ...
Read more >
Quick Guide on Loading Initial Data with Spring Boot - Baeldung
Spring Boot internally defaults this parameter value to create-drop if no schema manager has been detected, otherwise none for all other cases.
Read more >
Create and Drop APIs - EF Core - Microsoft Learn
APIs for creating and dropping databases with Entity Framework Core. ... database if it doesn't exist and initialize the database schema.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found