Model.init() doesn't work after dropDatabase() when Model is initialized before
See original GitHub issueIn 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:
- Created 5 years ago
- Reactions:2
- Comments:5
Top 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 >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Hi @vkarpov15
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()
anddropDatabase()
) => 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”.
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 😦 .