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

Indexes are not created when declared in Model JSON #103

Closed
ilianaza opened this issue Feb 25, 2015 · 11 comments
Closed

Indexes are not created when declared in Model JSON #103

ilianaza opened this issue Feb 25, 2015 · 11 comments

Comments

@ilianaza
Copy link

I just want to check whether creating indexes feature has been implemented or not. I followed documentation on http://docs.strongloop.com/display/public/LB/Model+definition+JSON+file#ModeldefinitionJSONfile-Indexes and tried to declare the following index in my model json file:

...
"base": "PersistedModel",
  "indexes": {
    "unique_registration_index": {
      "keys": {
        "playerId": 1,
        "eventId": 1
      },
      "options": {
        "unique": true
      }
    }
  },
...

But when I check MongoDB, I don't see this index being generated.

  1. Has that feature been implemented?
  2. If not, what's the best way for me to restrict uniqueness for combination of fields (like playerId and eventId in my case)?
@raymondfeng
Copy link
Contributor

You'll have to run automigrate (drop and recreate collections/indexes) or autoupdate (ensureIndexes).

@ilianaza
Copy link
Author

@raymondfeng Thanks for the quick response. Saved my day!

@uzyn
Copy link
Contributor

uzyn commented Jun 7, 2015

@raymondfeng If that is the case shouldn't it be included as one of the boot scripts by default?

@nvcken
Copy link

nvcken commented Jun 12, 2015

I tried automigrate and autoupdate at boot scripts

module.exports = function(server) {
    var ds = server.dataSources.mongodb;
        ds.automigrate('Product');
    ds.autoupdate('Product', function () {
         });

Then all data of Product lost without preserve

@raymondfeng
Copy link
Contributor

Use autoupdate, NOT automigrate.

@nvcken
Copy link

nvcken commented Jun 12, 2015

Thanks 👍

@thaiat
Copy link

thaiat commented Oct 18, 2015

for what it worth here is my autoupdate script:

'use strict';
global.Promise = require('bluebird');

module.exports = function(server) {

    Promise.each(server.models(), function(model) {

        if (model.dataSource) {
            var autoupdate = Promise.promisify(model.dataSource.autoupdate);
            if (autoupdate) {
                return autoupdate.call(model.dataSource, model.modelName);
            }
        }
    });
};

@0candy
Copy link
Contributor

0candy commented Aug 9, 2016

Closing this issue as its been resolved.

@michaelfreund
Copy link

michaelfreund commented Oct 31, 2016

@thaiat thanks for the script. works like a charm. but what if you remove an index definition from a model.json? I recognized that indexes are not removed from db. @raymondfeng should autoupdate be a loopback boot script which is called on every boot?

@shashank-komuroju
Copy link

shashank-komuroju commented Dec 3, 2016

@thaiat I'm getting this error when the script is executed. Yet the indexes are created.
MongoError: collection already exists

@akvaliya
Copy link

@michaelfreund have you found solution for remove an index from db when remove from model.json?

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

No branches or pull requests

9 participants