[英]LoopBack Automigrate Call Order
我正在使用LoopBack自動遷移在應用程序啟動期間從模型創建MySQL數據庫表。 我的帳戶模型與我的信用模型定義了hasAndBelongsToMany關系,反之亦然。
我正在嘗試在帳戶中創建貸方,但是出現SQL錯誤。 “錯誤:ER_PARSE_ERROR:您的SQL語法有錯誤;請在與MySQL服務器版本相對應的手冊中找到在第1行的'3 ORDER BY id
LIMIT 1'附近使用的正確語法”
根據我的日志記錄,似乎自動遷移沒有按照我的automigrate.js文件中定義的順序發生,並且LoopBack分配了一些任意順序,這導致在嘗試向其添加信用額時未創建“帳戶”表...
環回的順序根據我的日志記錄創建了自動遷移表,它首先創建了帳戶,然后是creditaccounts,然后是credit ...我需要它是Account,credits和creditaccounts。
因此,我的問題是,在使用自動遷移時,如何控制LoopBack創建數據庫表的順序? 還是我錯過了什么?
我的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
})
});
});
});
解決方案與自動遷移無關,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.