loopback icon indicating copy to clipboard operation
loopback copied to clipboard

Cannot migrate models not attached to this datasource: Application

Open margagn opened this issue 10 years ago • 9 comments

I just followed the following tutorial (https://docs.strongloop.com/display/public/LB/Connect+your+API+to+a+data+source) and I was able to see the CoffeeShop table in my SQL Server Express 2014. However, as soon as I change the dataSource for the built-in models (User, Application, etc.) I get the following error: "Cannot migrate models not attached to this datasource: Application".

Any idea what is going on?

Thank you!

create-sample-model.js

module.exports = function(app) {

  app.dataSources.sqlserver.automigrate(['User', 'Application', 'Role', 'ACL', 'RoleMapping', 'AccessToken'], function(err) { 
    if (err) throw err;
  });

  app.dataSources.sqlserver.automigrate('CoffeeShop', function(err) {
    if (err) throw err;

    app.models.CoffeeShop.create([
      {name: 'Bel Cafe', city: 'Vancouver'},
      {name: 'Three Bees Coffee House', city: 'San Mateo'},
      {name: 'Caffe Artigiano', city: 'Vancouver'},
    ], function(err, coffeeShops) {
      if (err) throw err;

      console.log('Models created: \n', coffeeShops);
    });
  });
};

model-config.json

{
  "_meta": {
    "sources": [
      "loopback/common/models",
      "loopback/server/models",
      "../common/models",
      "./models"
    ],
    "mixins": [
      "loopback/common/mixins",
      "loopback/server/mixins",
      "../common/mixins",
      "./mixins"
    ]
  },
  "User": {
    "dataSource": "sqlserver"
  },
  "AccessToken": {
    "dataSource": "sqlserver",
    "public": false
  },
  "ACL": {
    "dataSource": "sqlserver",
    "public": false
  },
  "RoleMapping": {
    "dataSource": "sqlserver",
    "public": false
  },
  "Role": {
    "dataSource": "sqlserver",
    "public": false
  },
  "CoffeeShop": {
    "dataSource": "sqlserver"
  }
}

datasources.json

{
  "sqlserver": {
    "host": "MyMachine",
    "database": "demo",
    "username": "User",
    "password": "password",
    "name": "sqlserver",
    "connector": "mssql",
    "debug": true,
    "options": {
      "encrypt": false,
      "instance": "SQLEXPRESS"
    }
  }
}

margagn avatar Feb 17 '16 04:02 margagn

@margagn: You're getting that error because in your model-config.json file, Application model is missing and thus not bound to a datasource.

The file /server/model-config.json configures LoopBack models, for example it binds models to data sources and specifies whether a model is exposed over REST. The models referenced in this file must be either a built-in models or custom models defined by a JSON file in the common/models/ folder.

(Read more here)

Adding following to your model-config.json file will resolve the issue.

  "Application": {
    "dataSource": "sqlserver"
  },

Interesting reading on: How to use built-in models & Creating database schema from Models.

Hope that helps, enjoy! Closing for now, please feel free to re-open if you above solution does not work for you/have more questions. Thank you.

gunjpan avatar Feb 17 '16 19:02 gunjpan

I ran into this problem about a year after this was closed, and this suggestion resolved it -- so doesn't this need to be fixed in the tutorial?

mprogers avatar Feb 04 '17 22:02 mprogers

@mprogers Would you like to contribute a fix to the tutorial? I can help you get it landed if you're interested.

superkhau avatar Feb 07 '17 08:02 superkhau

@superkhau The documentation seems to be missing so many issues. I think i can understand your situation you might be facing a lot of issues. Assign me. I can fix this problem.

vijayksingh avatar Jul 18 '17 11:07 vijayksingh

@perceptron007 Would you like to contribute some documentation via pull requests? Ping me directly in there and let's get your proposed document fixes landed.

superkhau avatar Jul 18 '17 16:07 superkhau

This is still an issue. Just following the tutorial. FWIW I'm using Mysql. in model-config.json

"CoffeShop": {
   "dataSource": "mysqlDs",
   "public": true
 }

in datasources.json

  "mysqlDs": {
    "host": "127.0.0.1",
    "port": 3306,
    "url": "",
    "database": "getting_started",
    "password": "changeit",
    "name": "mysqlDs",
    "user": "root",
    "connector": "mysql"
  }

Error Message when running node . Error: Cannot migrate models not attached to this datasource: CoffeeShop

mpereira-dev avatar Nov 11 '17 02:11 mpereira-dev

Any solution to this issue??

rror: Cannot migrate models not attached to this datasource: User AccessToken ACL RoleMapping Role

VisionMkhabela avatar Jul 26 '18 20:07 VisionMkhabela

Any solution to this issue??

rror: Cannot migrate models not attached to this datasource: User AccessToken ACL RoleMapping Role

Did you try the fix mentioned by gunjpan commented on Feb 17, 2016?

adambant avatar Oct 04 '18 22:10 adambant

Hey, in 2020, I have the same problem. I followed the tute step by step and I use the strongloop sql.

Error: Cannot migrate models not attached to this datasource: CoffeeShop

/server/datasources.js "localDs": { "host": "127.0.0.1", "port": 3306, "url": "", "database": "demo", "password": "...", "name": "localDs", "user": "...", "connector": "mysql" } }

server/model-config.js "Coffeeshop": { "dataSource": "localDs", "public": true }

pizza-hut avatar Apr 01 '20 15:04 pizza-hut