0

I am using LoopBack auto migrate to create MySQL database tables from models during app boot. My Account model define a hasAndBelongsToMany relationship with my Credit model, and visa versa.

I am trying to create a credit in account, but I get an SQL error. "Error: ER_PARSE_ERROR: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '3 ORDER BY id LIMIT 1' at line 1"

According to my logging, it seems that the automigrate does not happen in order defined in my automigrate.js file, and that some arbitrary order is assigned by LoopBack, which causes the Account table to not be created when trying to add the credit to it...

The order that loopback creates automigrate table according to my logging, it first creates accounts, then creditaccounts, then credit... I need it to be account, credits, then creditaccounts.

My question is thus, how can I control the order in which LoopBack creates database tables when using auto migrate? Or am I missing something?

Condensed version of my server/boot/automigrate.js file:

app.automigrate('account', function(err) {
  server.models.Account.create({id:1}, function(err, record) {
    //handle errors here
  });
});

app.automigrate('credit', function(err) {
  server.models.Credit.create({id:1}, function(err, record) {
    //handle errors here
  });
});

app.automigrate('creditaccount', function(err) {
  server.models.Account.findById({id:1}, function(err, account) {
    server.models.Credit.findById({id:1}, function(err, credit) {
      account.credits.add(credit, function(err, creditaccount) {
        //error handling
      })
    });        
  });
});
Mozfet
  • 359
  • 3
  • 12
  • ", it seems that the automigrate does not happen in order defined in my automigrate.js file"... Despite the fact that you solved your syntax error there is no guarantee that your code will execute in order that is written. It is possible but it doesn't have to be that way. Loopback does not have any internal arbitrary execution order. You should read more about nodejs, understand event loop, non-blocking code and callbacks. If you need specific execution order you have to write your code differently. – A.Z. May 14 '15 at 13:14
  • Thanks A.Z. I understand the asyncronous nature of LoopBack and callbacks, but cannot find any examples of doing automigrate with related models, and the documentation is thin... how would you do this? – Mozfet May 16 '15 at 09:36
  • Maybe this answer can help you more with migrations http://stackoverflow.com/questions/29974947/migrating-built-in-models-to-databases/30038210#30038210 – A.Z. May 18 '15 at 08:23

1 Answers1

1

The solution has nothing to do with automigrate, the SQL error was because I was trying to call Account.findById with an object as first parameter, instead of a primitive parameter for id... thus it should be:

app.automigrate('account', function(err) {
  server.models.Account.create({id:1}, function(err, record) {
  //handle errors here
  });
});

app.automigrate('credit', function(err) {
  server.models.Credit.create({id:1}, function(err, record) {
    //handle errors here
  });
});

app.automigrate('creditaccount', function(err) {
  server.models.Account.findById(1, function(err, account) {
    server.models.Credit.findById(1, function(err, credit) {
      account.credits.add(credit, function(err, creditaccount) {
        //error handling
      })
    });        
  });
});
Mozfet
  • 359
  • 3
  • 12