Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix #222: Mongo::Error::OperationFailure: E11000 duplicate key error index. #225

Merged
merged 3 commits into from
Sep 8, 2016

Conversation

dblock
Copy link
Collaborator

@dblock dblock commented Sep 7, 2016

This is a proposed fix for #222. The problem is that we try to create records with a blank indexed field that generates blank slugs, which causes the unique index to break. We remove the default value of [] and store a nil slug that works with a sparse: true index.

I'd love a code review from @johnnyshields or @digitalplaywright please.

@Dan-Nolan
Copy link

Dan-Nolan commented Sep 7, 2016

Could you also add in a check to see if to_url resolves to a nil slug? I ran into a problem with this specifically when users were inputting values like '-'. The expression'-'.to_url resolves to an empty string as well, which could also cause this problem.

@dblock
Copy link
Collaborator Author

dblock commented Sep 7, 2016

Good point @Dan-Nolan, that seems to work, see specs in 218d257.

@Dan-Nolan
Copy link

Sweet! Thanks for checking that out!

@johnnyshields
Copy link
Member

johnnyshields commented Sep 7, 2016

The bigger question is, should we allow _slugs to be blank? I think we should (esp for legacy reasons) by using a sparse index on _slugs so that undefined is ignored for uniqueness purposes.

I think the logic to generate a fallback slug should be handled by the implementer on a case-by-case basis. I'm not convinced that using the model name as a default fallback is an elegant solution.

@dblock
Copy link
Collaborator Author

dblock commented Sep 8, 2016

@johnnyshields You think that sparse: true on the index would work?

@johnnyshields
Copy link
Member

johnnyshields commented Sep 8, 2016

@dblock sparse indexes will ignore documents which do not have the _slugs field set, hence are good for incorporating legacy documents. However, if the value of _slugs is nil (i.e. set in db to null) or [] then the unique index error can occur. As long as we're not setting _slugs in the DB for legacy and non-slugged docs then a sparse index would work.

https://docs.mongodb.com/manual/core/index-sparse/

As of MongoDB 3.2 there's a better option called "partial indexes" (sparse index on steroids) where you can exclude values nil and [] from the index as well.

https://docs.mongodb.com/manual/core/index-partial/

@dblock
Copy link
Collaborator Author

dblock commented Sep 8, 2016

@johnnyshields I updated this PR. The problem is that we default slugs to []. If we remove that we can start setting nils. I think it's better, would like your opinion.

@johnnyshields
Copy link
Member

@dblock this needs to be tested. my understanding is that sparse indexes consider "nil" (null) as a value if it's explicitly set in the db, so be careful. I've filed an issue with Mongoid that they need a way to distinguish between nil and unset.

@dblock
Copy link
Collaborator Author

dblock commented Sep 8, 2016

@johnnyshields: This is tested here, note that I am creating an index in spec_helper.

@dblock
Copy link
Collaborator Author

dblock commented Sep 8, 2016

I hear you @johnnyshields on 3.2.0, we could default to [] and require that version and make a more special index, but that's a whole other ballgame and will require people to migrate a massive amount of indexes.

The code in Mongoid doesn't set the value to nil, it leaves it out. At least that's what I see.

@dblock dblock force-pushed the fix-222 branch 4 times, most recently from b46d738 to 67433a2 Compare September 8, 2016 14:13
@dblock
Copy link
Collaborator Author

dblock commented Sep 8, 2016

Ok, I am pretty happy with what's in this PR.

@johnnyshields: do you think I am doing it all wrong or can I merge this?

@Dan-Nolan: the implementation has changed and since we last spoke, check this out.

@BorisBresciani you opened #222, please check this out.

@johnnyshields
Copy link
Member

@dblock looks fine to me.

@Dan-Nolan
Copy link

@dblock Looks great to me; very well documented too! Handling fallback slugs in a case-by-case basis makes sense, and in our case, we ended up not allowing values to resolve to empty stringex urls. It was bad data for us anyhow.

@dblock dblock merged commit 382b099 into mongoid:master Sep 8, 2016
@dblock dblock deleted the fix-222 branch September 8, 2016 17:03
@dblock
Copy link
Collaborator Author

dblock commented Sep 11, 2016

FYI was released in 5.3.0

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants