Search code examples
loopbackjsstrongloop

Why auto-migration doesn't create any properties in a Collection? How to make it to create them?


I have created models using slc loopback:model tool. Now I want Loopback to create corresponding MongoDB collections, that is to perform auto-migration.

One of the models is a Client model whose base class is a User model. That means that client/models/client.json is just empty because all its properties (fields) are inherited from User:

{
  "name": "Client",
  "plural": "Clients",
  "base": "User",
  "idInjection": true,
  "properties": {},
  "validations": [],
  "relations": {},
  "acls": [],
  "methods": []
}

So I think to myself that if I make an auto-migration, Loopback finds all User properties and creates Client collection with them. But it doesn't! My Client collection has only _id property.

Here is a my code for auto-migration:

module.exports = function(app) {
    app.dataSources.mongodb.automigrate('Client', function(err) {
        if (err) throw err;
    });
};

My question:

Why Loopback doesn't use User model properties for my Client model? How to auto-migrate so that Loopback will create correct collection?


Solution

  • automigrate is used to migrate model data into tables i.e. Model name as tablename and Model's properties as table columns.

    Now as you are using MongoDB, it drops and creates indexes as written in documentation. This is because MongoDB is schemaless.

    So, probably you can avoid automigration and insert new documents directly.