简体   繁体   English

LoopBack自动迁移呼叫顺序

[英]LoopBack Automigrate Call Order

I am using LoopBack auto migrate to create MySQL database tables from models during app boot. 我正在使用LoopBack自动迁移在应用程序启动期间从模型创建MySQL数据库表。 My Account model define a hasAndBelongsToMany relationship with my Credit model, and visa versa. 我的帐户模型与我的信用模型定义了hasAndBelongsToMany关系,反之亦然。

I am trying to create a credit in account, but I get an SQL error. 我正在尝试在帐户中创建贷方,但是出现SQL错误。 "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" “错误:ER_PARSE_ERROR:您的SQL语法有错误;请在与MySQL服务器版本相对应的手册中找到在第1行的'3 ORDER BY id LIMIT 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... 根据我的日志记录,似乎自动迁移没有按照我的automigrate.js文件中定义的顺序发生,并且LoopBack分配了一些任意顺序,这导致在尝试向其添加信用额时未创建“帐户”表...

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. 环回的顺序根据我的日志记录创建了自动迁移表,它首先创建了帐户,然后是creditaccounts,然后是credit ...我需要它是Account,credits和creditaccounts。

My question is thus, how can I control the order in which LoopBack creates database tables when using auto migrate? 因此,我的问题是,在使用自动迁移时,如何控制LoopBack创建数据库表的顺序? Or am I missing something? 还是我错过了什么?

Condensed version of my server/boot/automigrate.js file: 我的server / 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
      })
    });        
  });
});

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: 解决方案与自动迁移无关,SQL错误是因为我试图使用一个对象作为第一个参数而不是id的原始参数来调用Account.findById,因此应该是:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM