fix(database): cannot overwrite view bug#1464
Draft
ChrisPdgn wants to merge 1 commit into
Draft
Conversation
ioanniskemerlis
approved these changes
May 6, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This is my attempt to fix the "Cannot overwrite ... model" race condition
Concurrent calls to
createViewwith the sameviewNamecaused intermittent failures:This is a check-then-act race. Two calls both see
this.views[viewName]as unset, then both try to register the same Mongoose model and create the same MongoDB view. The second fails because:mongoose.model(name, schema)throwsOverwriteModelErrorif the name is already registered.createCollection({ viewOn })can fail withNamespaceExistsif the view already exists in MongoDB.Typical triggers:
CreateResourceAccessListcalls that resolve to the same hashedviewName.database:create:viewbus events.recoverViewsFromDatabaserunning manycreateViewcalls in parallel.Solution
Two complementary mechanisms in the Mongoose adapter:
1. Promise coalescing (same process)
A
pendingViewCreationsmap tracks in-flight work perviewName. A second caller awaits the same promise instead of starting duplicate registration /createCollection.2. Catch-and-recover (multi-instance and leftovers)
MongooseSchemaconstructor: catchOverwriteModelErrorand use the existingmongoose.model(name).createCollection: ignoreNamespaceExists/ “already exists” style errors (aligned with how SQL adapters tolerate duplicate view creation).Testing results
I have observed that this error is always thrown when 2 database instances are deployed and tested this fix in that case. The error wasn't thrown successfully.
What kind of change does this PR introduce?
Does this PR introduce a breaking change?
The PR fulfills these requirements:
mainbranchfix #xxx, where "xxx" is the issue number)If adding a new feature, the PR's description includes:
Other information: