Search code examples
mysqlnode.jsloopbackjs

MySQL table auto creation is not working in Loopback node API framework after installing MySQL connectors


1.I have created a demo application in loopback using "lb demo" command.

2.Then, successfully installed MySQL connectors using "npm install loopback-connector-mysql --save" this command.

3.Then create user model with firstName and lastName properties.

  1. Trying to post data using GUI explorer on "http://localhost:3000/explorer/" but it throws "TABLE NOT EXIST ERROR".

or can we need to manually create MySQL tables.?

Please suggest any solution. I tried a lot for this but not able to resolve the problem.

Thanks in advance.


Solution

  • According to your steps you never did try to auto create them.

    To invoke automigrate try creating server/bin/automigrate.js with the following code, then run node server/bin/automigrate in your project directory

    var server = require('./server');
    var ds = server.dataSources.MYDATASOURCE;
    var lbTables = ['myUserModel', 'anotherModelIWantToCreate'];
    ds.automigrate(lbTables, function(er) {
      if (er) throw er;
      console.log('Loopback tables [' - lbTables - '] created in ', ds.adapter.name);
      ds.disconnect();
    });
    

    https://loopback.io/doc/en/lb3/Creating-a-database-schema-from-models.html

    Warning: Auto-migration will drop an existing table if its name matches a model name. When tables with data exist, use auto-update to avoid data loss.