1
votes

I can't get automigrate and autoupdate to work.

I created my project, datasource and model. All looks good, but I can't get automigrate to work. If I go to my SQL Server and create the table/schema then all the GET/POST methods work fine.

I went to the server/boot directory. there are currently two files in there authentication and root.

I created a new file called automigrate.js

'use strict';

 module.exports = function (app) {
   app.dataSources.mysql.automigrate();
   console.log("Performed automigration.");
}

When I run it I get an error :

C:\Users\ajmal\loopback\telematics\server\server.js:31 if (err) throw err; ^

TypeError: Cannot read property 'automigrate' of undefined

at Object.module.exports [as func] (C:\Users\ajmal\loopback\telematics\server\boot\automigrate.js:9:26)
at C:\Users\ajmal\loopback\telematics\node_modules\loopback-boot\lib\executor.js:316:22

1
are you sure that your datasource is called mysql? Check your file datasources.json and make sure that you're using exactly the same datasource name - akkonrad
This was the issue. I was using the connector parameter as the name instead of name from my datasourses.json. Tx - Ajmal Nazir

1 Answers

1
votes

You can use grunt-loopback-auto module or build your own by following the document at https://loopback.io/doc/en/lb3/Creating-a-database-schema-from-models.html#auto-update

Notes:

  • automigrate will drop and re-create the tables. Therefore, data will be lost. It's terrible on production.
  • autoupdate calculates the difference between the LoopBack model and the database table definition and alters the table accordingly.

So, we should use autoupdate in almost cases.