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!
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);
});
});
};
{
"_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"
}
}
{
"sqlserver": {
"host": "MyMachine",
"database": "demo",
"username": "User",
"password": "password",
"name": "sqlserver",
"connector": "mssql",
"debug": true,
"options": {
"encrypt": false,
"instance": "SQLEXPRESS"
}
}
}
@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.
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 Would you like to contribute a fix to the tutorial? I can help you get it landed if you're interested.
@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.
@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.
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`
Any solution to this issue??
rror: Cannot migrate models not attached to this datasource: User AccessToken ACL RoleMapping Role
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?
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
}
Most helpful comment
@margagn: You're getting that error because in your
model-config.jsonfile,Applicationmodel is missing and thus not bound to a datasource._(Read more here)_
Adding following to your
model-config.jsonfile will resolve the issue.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.