OverwriteModelError with mocha 'watch'

1,395 views
Skip to first unread message

Davis Ford

unread,
Dec 18, 2012, 11:28:09 AM12/18/12
to mongoo...@googlegroups.com
Hi Aaron, 

I thought I'd follow up here on - since it may be interesting for a wider audience.  I'll add a backref to the github issue

https://github.com/LearnBoost/mongoose/issues/1251#issuecomment-11466245

I'm still not totally clear on this - so I'm hoping you can clarify it for me.  The problem in a nutshell: when I upgraded to the latest mongoose (3.5.1 I think), my tests started failing on a re-run with OverwriteModelError (i.e. using mocha's --watch option http://visionmedia.github.com/mocha/#watch-option) -- first pass through on the tests passes.

You said

This is caused by a programming error. Calling either mongoose.model() or connection.model() with the same name and schema multiple times does not result in an error. Passing the name of a model that exists with a different schema produces the error. Overwriting models was never actually permitted.
If mocha watch re-requires your schema and calls mongoose.model(preExistingName, newSchema) then yes you'll get an error now. Previously in this scenario, mongoose wasn't compiling a new model at all, it would just swallow the programming error and return the old model, which is wrong behavior.

...but that isn't what I'm doing (AFAIK).  In the snippet I posted https://github.com/LearnBoost/mongoose/issues/1251#issuecomment-11459836 I only ever call mongoose.model('User', userSchema) once in user.js, and I never create a different schema and try to create a model named 'User' with that different schema.

I've done some poking around to see how other people are setting up testing with mongoose + mocha, and it is all the same pattern.  They require('mongoose') at the top of the test, and do mongoose.connect('mongodb://localhost/test') as a direct expression, or as part of before( ) / after ( ) -- but this pattern doesn't seem to work anymore with this new enforcement of OverwriteModelError

So, I'm taking a look at your internal mongoosejs source tests (as you referenced in the github issue) to see how you do your own tests, and see if I can create a workaround, but I'm still confused as to why this occurs, since I'm not trying to create the model multiple times with a different schema.

Anyone else running into this with the latest release? 

Illimar Tambek

unread,
Jan 10, 2013, 7:55:20 AM1/10/13
to mongoo...@googlegroups.com
I managed to get my tests working again by wrapping the mongoose.model() inside a try catch statement like this:

  try {
    var model = mongoose.model('User');
  } catch (err) {
    var model = mongoose.model('User', User);
  } 

It's not a solution to the problem, it's a workaround and I'd much rather not use it, but at least I can continue developing my app rather than spend anymore time on debugging this weird issue :D

I can now successfully run tests in my Locomotive app and use mocha with the --watch option

On Wednesday, January 9, 2013 11:46:32 PM UTC+2, Illimar Tambek wrote:
Me too, I have explained my issue here: https://github.com/LearnBoost/mongoose/issues/1251#issuecomment-12065100

On Monday, December 24, 2012 8:32:41 AM UTC+2, J Irving wrote:
Yes, I'm running into this.

I made a minimal example which I think demonstrates the problem:


cheers, J

Rémi Castaing

unread,
Apr 29, 2013, 4:12:42 PM4/29/13
to mongoo...@googlegroups.com
Hi,

But what if I'm actually working on the model and changing it. Then there no other way than using nodemon.

Or perhaps, by destroying the model and building a new one.

Cordialement, Rémi

Rémi Castaing

unread,
Apr 30, 2013, 4:56:38 AM4/30/13
to mongoo...@googlegroups.com
There is indeed another option: cleaning mongoose from all models and schemas:
I added :
mongoose.models = {};
mongoose.modelSchemas = {};
And it works fine. 
Message has been deleted

George Campbell

unread,
Apr 12, 2018, 8:39:07 PM4/12/18
to Mongoose Node.JS ODM
I had the same issue, and solved like this:
Check `mongoose.modelNames()` and determine whether to compile the model or just retrieve the already compiled model, as mocha --watch causes this issue.
   
mongoose.modelNames().indexOf("User") === -1 //if my model has not been compiled...
 
? mongoose.model("User", UserSchema) //...compile model and return it
 
: mongoose.connection.model("User"); //else it is already compiled, so return this model


now you return this as a function (replace "User" with argument, and UserSchema with the argument for your Schema) for `module.exports` and when you require it you call this function.

Reply all
Reply to author
Forward
0 new messages