Search code examples
mysqlnode.jsloopbackjs

Loopback autoupdate not creating custom models


I'm trying to create an app using Loopback (V3) and i've encountered a weird error.

I'm trying to create my Mysql tables using autoupdate(), but for my custom models it is not working.

This is what i've tried so far ->

var server = require('../server');
var ds = server.dataSources.db;

var models = ['test','Post','User', 'AccessToken', 'ACL', 'RoleMapping', 'Role'];

ds.isActual(models, function(err, actual) {
  if (!actual) {
    ds.autoupdate(null, function(err,result){
      console.log("DONE!");
      console.log(result);
    });
  };
});

The script works. If the database is empty it will create tables for all EXCEPT test and Post. Those are my custom models, the others are built into loopback.

Is it because of model type? (tried Model and PersistedModel) or is it something else? I even tried without the isActual check and still nothing.


Solution

  • I would recommend that you keep two separate arrays for built-in models and custom models and write code like following, that way you could know where the issue is. also, I think there is an error in your code near ds.autoupdate(null, fun..... please follow according to the below code

    var builtInModels = ['AccessToken', 'ACL', 'RoleMapping','Role'];
    var userDefinedModels = ['Post','test'];
    // migrate built-in models
    dataSource.isActual(builtInModels, function (err, actual) {
      if (!actual) {
        dataSource.autoupdate(builtInModels, function (err, result) {
          if(err) console.log(err);
          console.log('Datasource Synced: Built in models');
        });
      }
    });
    
    // migrate userdefined models
    dataSource.isActual(userDefinedModels, function (err, actual) {
      if (!actual) {
        dataSource.autoupdate(userDefinedModels, function (err, result) {
          if (err) console.log(err);
          console.log('Datasource Synced: User defined models');
        });
      }
    });