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

E11000 duplicate key error collection #307

Closed
DeanKamali opened this issue Dec 16, 2019 · 7 comments
Closed

E11000 duplicate key error collection #307

DeanKamali opened this issue Dec 16, 2019 · 7 comments
Labels

Comments

@DeanKamali
Copy link

DeanKamali commented Dec 16, 2019

I'm trying to create a number of new roles and I keep getting this error

> Roles.createRole('admin');
MongoError: E11000 duplicate key error collection: meteor.roles index: name_1 dup key: { name: null }


> Roles.createRole('provider');
MongoError: E11000 duplicate key error collection: meteor.roles index: name_1 dup key: { name: null }

alanning:[email protected]

Any ideas?

@SimonSimCity
Copy link
Member

Does this happened after updating your roles collection from 1.x, or at least you still have the index on the name field still on your roles collection?

@DeanKamali
Copy link
Author

DeanKamali commented Dec 16, 2019

Yes, it started to happen after the upgrade, I'm not sure what version I had before 1.x or 2.x checking git, it only says alanning:roles from before.

I only have 3 roles (admin, patient, provider) in roles collection

/** Meteor shell **/

> Roles.getAllRoles().fetch()
[ { _id: 'aWJYaTEjbLzYCJTdu', name: 'admin' },
  { _id: 'owAd3tYj5ZtKWCLTn', name: 'patient' },
  { _id: 'ssYDT95Gcv2xJvKBs', name: 'provider' } ]

> Roles.addUsersToRoles('q5jdXMSiTavQqQkgn', 'admin'); // Trying to add a user to role
Error: Role 'admin' does not exist.
    at evalCommandPromise.then (packages/shell-server/shell-server.js:249:21)
    at runBound (domain.js:314:12)
    at bound (domain.js:301:14)
    at defaultEval (repl.js:240:29)
    at ContextifyScript.Script.runInThisContext (vm.js:50:33)
    at repl:1:-55
    at Object.addUsersToRoles (packages/alanning:roles/roles/roles_common.js:372:11)
    at Array.forEach (<anonymous>)
    at packages/alanning:roles/roles/roles_common.js:379:13
    at Array.forEach (<anonymous>)
    at packages/alanning:roles/roles/roles_common.js:380:15
    at Object._addUserToRole (packages/alanning:roles/roles/roles_common.js:473:15)
> 

@SimonSimCity
Copy link
Member

SimonSimCity commented Dec 16, 2019

The roles you show me also are according to the first version of this package. Please run the update script before running any additional commands. First update to v2.0 and then to v3.0. I advise you to do it in two separate steps as proposed in the readme (https://github.com/Meteor-Community-Packages/meteor-roles#migration-to-30).

Please keep in mind that the update to version 1.x will still leave the old roles in place, which you have to remove manually until #300 is resolved.

@SimonSimCity
Copy link
Member

Let me know if there's anything else, but this should solve it. Please ask to reopen if it doesn't.

@DeanKamali
Copy link
Author

Well, here is what I ended up doing, I dropped roles and role-assignment and it got re-created when I added new roles.

Things started to work, however, I faced an issue with getUsersInRole returning an empty array all the time.

Turned out that my local MongoDB use newer version 4.2 and by downgrading to version 4.0.10
getUsersInRole method started to work.

@SimonSimCity
Copy link
Member

Oh! Thanks for notifying! Could you create a new issue for this?

@jamauro
Copy link

jamauro commented Jul 26, 2021

If anyone else comes across the same problem, the sequence of upgrading is important.

  1. Type meteor remove alanning:roles and then meteor add alanning:[email protected]
  2. Run the migration script in the shell https://github.com/Meteor-Community-Packages/meteor-roles#migration-to-30
  3. Then update your code e.g. add things like Roles.createRole. (If you do this step before the migration script you'll get the duplicate error key.)

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

No branches or pull requests

3 participants