If an insert fails, set isNew back to false so the save operation can be retried if desired #837

Merged
merged 1 commit into from Apr 15, 2012

3 participants

@boutell

Without this a second attempt to save tries to do an update operation on a nonexistent document. This allows one to make the slug more unique and try again on a unique index error. Responding to the actual unique index error allows us to avoid race conditions.

@boutell boutell If an insert fails, set isNew back to false so the save operation can…
… be retried if desired.

Without this a second attempt to save tries to do an update operation on a nonexistent document. This allows one to make the slug more unique and try again on a unique index error. Responding to the actual unique index error allows us to avoid race conditions.
ea37acc
@boutell

There's a gist here with a nice test program:

https://gist.github.com/2387479

@boutell

Here's another gist with a handy function to enhance any model with a self-extending slug behavior, such that unique index errors on the slug field are automatically retried with a random digit added to the slug. Works great as long as my commit is in place:

https://gist.github.com/2389267

Thanks again for a sweet ODM.

@rauchg rauchg merged commit d51e78a into Automattic:master Apr 15, 2012
@rauchg

Great catch. We need a test case for this to avoid regressions

@aheckmann aheckmann added a commit that referenced this pull request Apr 16, 2012
@aheckmann aheckmann fix isNew on sub doc after insertion error
relates to #837
f70be19
@boutell

This fix is not in 2.5.14. Will it be in the 2.5.x series? We have a project in which we figured it would soon be available. Thanks!

@boutell

I was able to work around this by setting isNew in my shiny new Mongoose plugin:

https://github.com/punkave/mongoose-uniqueslugs

It would be better of course not to tamper with the internals.

Thanks!

@aheckmann

sorry, i'll port it back.

@boutell

Thanks!

@aheckmann aheckmann added a commit that referenced this pull request May 24, 2012
@aheckmann aheckmann fix isNew on sub doc after insertion error
relates to #837
a4d33f3
@aheckmann

just published 2.6.5 which contains this fix

@boutell

Thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment