Sequelize 需要将一个已经存在的记录关联到新创建的记录

Sequelize need to associate an already existing record to newly created ones

我正在创建一个存储许多不同类型对象的应用程序,它们的独特属性存储在不同的 table 中,并且与 table 的关联与所有对象共享的一般属性对象并且 table 必须与 table 存储用户输入的已指定位置相关联。

我已经创建了 tables 及其关联,代码示例如下: 这是一般属性 table:

var AssetInfo = sequelize.define('AssetInfo', {
invoiceNumber: {type:Sequelize.STRING, allowNull:false},
purchaseOrder: {type:Sequelize.STRING, allowNull:false},
physicalCondition: {type:Sequelize.STRING, allowNull:false},
modelName: {type:Sequelize.STRING, allowNull:false},
supplier: {type:Sequelize.STRING, allowNull:false},
manufacturer: {type:Sequelize.STRING, allowNull:false},
status: {type:Sequelize.STRING, allowNull:false},
purchaseCost: {type:Sequelize.DOUBLE, allowNull:false},
acquiredDate: {type:Sequelize.DATE, allowNull:false},
lostOrStolen: {type:Sequelize.BOOLEAN, allowNull:false, defaultValue:false},
lOSDate: {type:Sequelize.DATE, allowNull:true},
usefulLife: {type:Sequelize.INTEGER, allowNull:true},
warrantyExpiry: {type:Sequelize.DATE, allowNull:false},
depreciateRate: {type:Sequelize.DOUBLE, allowNull:false},
scrapped: {type:Sequelize.BOOLEAN, allowNull:false, defaultValue:false},
dateScrapped: {type:Sequelize.DATE, allowNull:true},
scrappedTo: {type:Sequelize.STRING, allowNull:true},
sold: {type:Sequelize.BOOLEAN, allowNull:false, defaultValue:false},
dateSold: {type:Sequelize.DATE, allowNull:true},
sellPrice: {type:Sequelize.DOUBLE, allowNull:true},
priceLoss: {type:Sequelize.DOUBLE, allowNull:true},
currentCost: {type:Sequelize.DOUBLE, allowNull:false},
netRealisableValue: {type:Sequelize.DOUBLE, allowNull:false},
fairValue: {type:Sequelize.DOUBLE, allowNull:false},
picture: {type:Sequelize.STRING, allowNull:true},
description: {type:Sequelize.STRING, allowNull:true}},
{freezeTableName:true}
);

这是对象 table 和位置 table 之一:

var Computer = sequelize.define('Computer', {
serial: {type:Sequelize.STRING, allowNull:false},
user: {type:Sequelize.STRING, allowNull:false}},
{freezeTableName:true
});

var CompanyLocation = sequelize.define('CompanyLocation', {
companyName: {type:Sequelize.STRING, allowNull:false},
site: {type:Sequelize.STRING, allowNull:false}},
{freezeTableName:true}
);

CompanyLocation.hasMany(AssetInfo);
Computer.hasMany(AssetInfo);
AssetInfo.belongsTo(Computer);

我能够通过以下方式创建对象和一般信息的关联 table:

function addComputer(inv, pur, phys, mod, sup, manu, stat, purC, acD, useLife,warr, dep, cur, netRe, fair, pic, desc, num, use){
sequelize.Promise.all([
    Computer.create({serial : num,user : use}),
    AssetInfo.create({invoiceNumber : inv, purchaseOrder: pur,
        physicalCondition : phys, modelName : mod, supplier : sup ,
        manufacturer : manu, status : stat, purchaseCost : purC,
        acquiredDate : acD, usefulLife : useLife, warrantyExpiry : warr,
        depreciateRate : dep, currentCost : cur, netRealisableValue : netRe,
        fairValue: fair, picture : pic, description : desc})
]).spread(function (comp, assIn) {
    return comp.addAssetInfo(assIn);
});
}

并尝试过诸如按名称查找预制位置并尝试将其与新创建的对象及其一般信息相关联的操作 table 但非常失败。

我在文档或其他论坛中找不到任何可以使代码正常工作的类似内容,只是无法将它们关联起来。任何帮助将不胜感激。

我试过将预设位置导入到原创函数中,如:

 function addComputer(inv, pur, phys, mod, sup, manu, stat, purC, acD, useLife,warr, dep, cur, netRe, fair, pic, desc, num, use){
sequelize.Promise.all([
    Computer.create({serial : num,user : use}),
    AssetInfo.create({invoiceNumber : inv, purchaseOrder: pur,
        physicalCondition : phys, modelName : mod, supplier : sup ,
        manufacturer : manu, status : stat, purchaseCost : purC,
        acquiredDate : acD, usefulLife : useLife, warrantyExpiry : warr,
        depreciateRate : dep, currentCost : cur, netRealisableValue : netRe,
        fairValue: fair, picture : pic, description : desc})
]).spread(function (comp, assIn) {
    return comp.addAssetInfo(assIn);
}).**Then(function(assIn)
   var locate = Location.findById(#);
   assIn.addLocation(locate));**
}

第 seond 行被标记为无法访问的文本,不会 运行

然后我尝试创建一个单独的函数来更新它们:

function addLocate(){
    var locate = Location.findById(#);
    var assIn = AssetInfo.findById(#);
    assIn.addLocation(locate);
}

给出了 assIn.addLocation 不是函数的错误(另请注意,我重写了这段代码,没有直接复制,删除了原来的代码,因为它不起作用,所以所有语法都不准确,例如 # 是一个number, addLocation 是 addCompanyLocation)

findById returns 承诺,就像 create 一样 - 您必须等待查询完成

function addComputer(inv, pur, phys, mod, sup, manu, stat, purC, acD, useLife, warr, dep, cur, netRe, fair, pic, desc, num, use) {
     sequelize.Promise.all([
         Computer.create({
             serial: num,
             user: use
         }),
         AssetInfo.create({
             invoiceNumber: inv,
             purchaseOrder: pur,
             physicalCondition: phys,
             modelName: mod,
             supplier: sup,
             manufacturer: manu,
             status: stat,
             purchaseCost: purC,
             acquiredDate: acD,
             usefulLife: useLife,
             warrantyExpiry: warr,
             depreciateRate: dep,
             currentCost: cur,
             netRealisableValue: netRe,
             fairValue: fair,
             picture: pic,
             description: desc
         })
     ]).spread(function(comp, assIn) {
         return comp.addAssetInfo(assIn);
     }).then(function(assIn) {
        return Location.findById(#).then(function (locate) {
            return assIn.addLocation(locate)); 
        });
    });
 }