LoopBack自动发送呼叫顺序

时间:2015-05-13 09:15:25

标签: javascript mysql model loopbackjs strongloop

我正在使用LoopBack自动迁移在应用程序启动期间从模型创建MySQL数据库表。我的帐户模型定义了与我的信用模型的hasAndBelongsToMany关系,反之亦然。

我正在尝试在帐户中创建一个信用卡,但我收到一个SQL错误。 "错误:ER_PARSE_ERROR:您的SQL语法中有错误;查看与您的MySQL服务器版本对应的手册,以便在#3; ORDER BY id LIMIT 1'附近使用正确的语法。在第1行"

根据我的日志记录,似乎automigrate不会按照我的automigrate.js文件中定义的顺序发生,并且LoopBack会分配一些任意顺序,这会导致在尝试添加时不会创建Account表归功于它...

loopback根据我的日志记录创建automigrate表的顺序,它首先创建帐户,然后是creditaccounts,然后是credit ...我需要它是帐户,信用,然后是creditaccounts。

我的问题是,如何在使用自动迁移时控制LoopBack创建数据库表的顺序?或者我错过了什么?

我的服务器/ boot / automigrate.js文件的简化版本:

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
      })
    });        
  });
});

1 个答案:

答案 0 :(得分:1)

该解决方案与automigrate无关,SQL错误是因为我试图用一个对象作为第一个参数来调用Account.findById,而不是id的原始参数...因此它应该是:

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
      })
    });        
  });
});
相关问题